http://momentjs.com/ or https://date-fns.org/
From Moment docs:
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // =1
or to include the start:
a.diff(b, 'days')+1 // =2
Beats messing with timestamps and time zones manually.
Depending on your specific use case, you can either
- Use
a/b.startOf('day')
and/ora/b.endOf('day')
to force the diff to be inclusive or exclusive at the “ends” (as suggested by @kotpal in the comments). - Set third argument
true
to get a floating point diff which you can thenMath.floor
,Math.ceil
orMath.round
as needed. - Option 2 can also be accomplished by getting
'seconds'
instead of'days'
and then dividing by24*60*60
.