154

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 that date is greater than or equal to today's date, excluding the time when comparing.

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

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

Sample isSame() from docs

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

Also I am looking for something like isSame() and isAfter() combined as one statement.

I need to compare using moment.js only.Please do not suggest plain javascript date comparison.

NoobGeek
  • 1,661
  • 2
  • 10
  • 7

7 Answers7

254

The docs are pretty clear that you pass in a second parameter to specify granularity.

If you want to limit the granularity 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

As the second parameter determines the precision, and not just a single value to check, using day will check for year, month and day.

For your case you would pass 'day' as the second parameter.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • 2
    Thanks! seems like i don't have to convert to string. But do we have any common method for isSame() and isAfter() – NoobGeek Apr 07 '15 at 15:15
  • 2
    I have already used that. Thanks! just wanted to see if there is a common function, not that it is difficult to use both. – NoobGeek Apr 08 '15 at 06:35
  • @kabirbaidhya, before making sizable unsolicited edits, it is considered polite to post a comment on questions and answers noting issues. I don't feel that the edits you made were necessary, so I rolled them back. If you feel that the information was important, please consider posting your own answer. – zzzzBov Mar 30 '16 at 13:24
  • 4
    Just to complement: "As the second parameter determines the precision, and not just a single value to check, using day will check for year, month and day." – eveevans Sep 02 '16 at 01:01
  • This won't work on firefox if you change the format from `YYYY-MM-DD` – dman Sep 16 '16 at 05:49
  • 6
    @NoobGeek There is now isSameOrBefore() and isSameOrAfter() from version 2.10.7 onwards. – WhiteHotLoveTiger Nov 04 '16 at 13:35
60

Meanwhile you can use the isSameOrAfter method:

moment('2010-10-20').isSameOrAfter('2010-10-20', 'day');

Docs: https://momentjs.com/docs/#/query/is-same-or-after/

sandrooco
  • 8,016
  • 9
  • 48
  • 86
15

In my case i did following code for compare 2 dates may it 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 Greater 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>
Amine Safi
  • 255
  • 4
  • 8
Renish Gotecha
  • 2,232
  • 22
  • 21
13

You could use startOf('day') method to compare just the date

Example :

var dateToCompare = moment("06/04/2015 18:30:00");
var today = moment(new Date());

dateToCompare.startOf('day').isSame(today.startOf('day'));
5
moment('2022-01-07').isAfter(moment().format('YYYY-MM-DD'), 'day');

checking 2022-01-07 this with current date

if current date is beyond 6th Jan 2022, it returns false!

Anupam Maurya
  • 1,927
  • 22
  • 26
4

For checking one date is after another by using isAfter() method.

moment('2020-01-20').isAfter('2020-01-21'); // false
moment('2020-01-20').isAfter('2020-01-19'); // true

For checking one date is before another by using isBefore() method.

moment('2020-01-20').isBefore('2020-01-21'); // true
moment('2020-01-20').isBefore('2020-01-19'); // false

For checking one date is same as another by using isSame() method.

moment('2020-01-20').isSame('2020-01-21'); // false
moment('2020-01-20').isSame('2020-01-20'); // true
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
0

moment('date').toDate().getTime() > moment()

patel jigar
  • 52
  • 1
  • 7