最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Comparing 2 dates with momentJS - Stack Overflow

programmeradmin8浏览0评论

I am trying to pare a DAY/TIME e.g. Monday 09:00:00 with the current time to see if I am past that point in the week. e.g. If it is now 05:00:00 on Monday it should return true however it is returning false everytime

var dayTime = Moment("Wednesday 17:00:00", "dddd HH:mm:ss");
var now = Moment(Moment.now(), "dddd HH:mm:ss");
console.log(Moment.utc(dayTime).isBefore(now)); //returns false all the time

I found the following similar questions but it didn't seem to fix the issue after formatting the time.

Comparing two times with Moment JS

When I replace the moment.now() with a string such as "Wednesday 17:00:00" it returns the expected result.

Any idea what I need to do to moment.now() in order for this to work correctly?

I am trying to pare a DAY/TIME e.g. Monday 09:00:00 with the current time to see if I am past that point in the week. e.g. If it is now 05:00:00 on Monday it should return true however it is returning false everytime

var dayTime = Moment("Wednesday 17:00:00", "dddd HH:mm:ss");
var now = Moment(Moment.now(), "dddd HH:mm:ss");
console.log(Moment.utc(dayTime).isBefore(now)); //returns false all the time

I found the following similar questions but it didn't seem to fix the issue after formatting the time.

Comparing two times with Moment JS

When I replace the moment.now() with a string such as "Wednesday 17:00:00" it returns the expected result.

Any idea what I need to do to moment.now() in order for this to work correctly?

Share Improve this question edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked Jun 13, 2016 at 21:44 GeraintGeraint 3,3923 gold badges27 silver badges41 bronze badges 2
  • I would not expect moment(moment.now(), "dddd HH:mm:ss") to produce the Moment you want; moment.now() return an epoch time representation, and you're passing in an entirely different format string. Try just moment(moment.now()) – Hamms Commented Jun 13, 2016 at 21:58
  • 1 moment.now shouldn't be used at all. It is an extension point that is not part of the public api. to get the current time just call moment() – Maggie Pint Commented Jun 13, 2016 at 22:04
Add a ment  | 

2 Answers 2

Reset to default 9

Moment.now can be used as an extension point, but it's really not a public API. To get the current time in momentjs you just call moment(). Note that all moment calls use lowercase moment.

To see if your date and time is before the current time, you would just call:

moment('01/01/2016', 'MM/DD/YYYY').isBefore(moment())

You would replace the date and format in question with your own.

I see that you have a date format that includes only day of week and time. Moment will parse this, but be aware that the behavior might not be what you expect. When I parse your date, I get Wednesday December 30, 2015. Exactly what day this lands on will vary by locale. In any case, I doubt that it is what you want. If at all possible, I would get year, month, and day.

If you would like to instead set the moment to Wednesday this week, set the day on the moment using .day(). For instance:

moment().day(3).format()
"2016-06-15T20:19:55-05:00"

For anyone who is interested, the code I posted in my question was changing the day/hour but was setting the year to 2015 which meant that it was always in the past.

To fix I separated out the Day and the Hour and set to moment. Then pared that with now. e.g.

moment().set({"Day": "Friday", "Hour": "17"}).isBefore(moment())
发布评论

评论列表(0)

  1. 暂无评论