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/or a/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 then Math.floor
, Math.ceil
or Math.round
as needed.
- Option 2 can also be accomplished by getting
'seconds'
instead of 'days'
and then dividing by 24*60*60
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…