Comparing two dates with jS

I am trying to compare DAY / TIME, for example. Monday 09:00:00 with the current time to see if I have passed this week. for example, if Monday is 05:00:00 on Monday, it should return true, but it returns false every time

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 didn't seem to fix the issue after formatting the time.

Comparing two times with Moment JS

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

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

+5
source share
2 answers

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

To fix, I selected Day and Hour and set the moment. Then compared it to now. eg.

 moment().set({"Day": "Friday", "Hour": "17"}).isBefore(moment()) 
+2
source

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

To find out if your date and time is up to the current time, you simply call:

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

You would replace the date and format with your question.

I see that you have a date format that includes only the day of the week and time. Moment will analyze this, but keep in mind that the behavior may not be what you expect. When I parse your date, I get Wednesday December 30, 2015. Exactly what day these lands will vary by region. In any case, I doubt that this is what you want. If at all possible, I get a year, a month and a day.

If you want to instead set this point on Wednesday this week, set the day at that moment using .day() . For instance:

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

All Articles