With momentjs, how do I tell if 2 moments represent the same day (not, necessarily, the same time)?
You can use both 'day'
and 'date'
to isSame
.
As the docs says:
Check if a moment is the same as another moment.
When including a second parameter, it will match all units equal or larger. Passing in
month
will checkmonth
andyear
. Passing inday
will checkday
,month
, andyear
.Like
moment#isAfter
andmoment#isBefore
, any of the units of time that are supported formoment#startOf
are supported formoment#isSame
.
In the docs of startOf
:
Note:
moment#startOf('date')
was added as an alias for day in 2.13.0
Here a working example with the lastest version (2.17.1):
var moment1 = moment('01/23/17', 'MM/D/YYYY');
var moment2 = moment('01/23/17', 'MM/D/YYYY');
console.log( moment1.isSame(moment2, 'day') ); // true
console.log( moment1.isSame(moment2, 'date') ); // true
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
Let's keep it simple.
moment(date1).format('L') === moment(date2).format('L')