How to compare only date in moment.js

I am new to moment.js. I have a date object, and it has some time associated with it. I just want to check if this date is greater than or equal to today's date , excluding comparison time.

var dateToCompare = 2015-04-06T18:30:00.000Z 

I just want to check if dateToCompare is equal to or greater than today's date. I checked the isSame of the .js moment, but it seems to take a string and only part of the date. But I do not want to convert my date to a string or manipulate it further. Because I'm worried that javascript might do something unexpected when converting this date to a string (like adding an offset or dst, etc.), Or maybe I'm wrong.

Sample isSame () from docs

 moment('2010-10-20').isSame('2010-10-20'); 

I am also looking for something like isSame () and isAfter () together as one operator.

I need to compare only with the moment.js parameter. Please do not offer simple javascript date matching.

+75
javascript momentjs
Apr 7 '15 at 14:49
source share
3 answers

The documents are pretty clear , which you pass in the second parameter to indicate the level of detail.

If you want to limit the detail to a unit other than milliseconds, pass the units as the second parameter.

 moment('2010-10-20').isAfter('2010-01-01', 'year'); // false moment('2010-10-20').isAfter('2009-12-31', 'year'); // true 

Since the second parameter determines the accuracy, and not just one value to check, using the day checks the year, month, and day.

In your case, you will pass 'day' as the second parameter.

+135
Apr 7 '15 at 14:54
source share

Meanwhile, you can use the isSameOrAfter method:

 moment('2010-10-20').isSameOrAfter('2010-10-20', 'day'); 
+28
Sep 14 '17 at 10:43 on
source share

In my case, I made the following code to compare 2 dates, maybe this will help you ...

 var date1 = "2010-10-20"; var date2 = "2010-10-20"; var time1 = moment(date1).format('YYYY-MM-DD'); var time2 = moment(date2).format('YYYY-MM-DD'); if(time2 > time1){ console.log('date2 is Greter than date1'); }else if(time2 > time1){ console.log('date2 is Less than date1'); }else{ console.log('Both date are same'); } 
 <script src="https://momentjs.com/downloads/moment.js"></script> 
+2
Jan 03 '19 at 10:45
source share



All Articles