Reputation: 5785
I have a date stored in YYYY-MM-DD. This date is used to create a moment called then
. I want to count days between this date and now
(moment).
When counting days I use: now.startOf('day').from(then.startOf('day'))
.
As an example, when testing using two dates with 3 days apart, it will be 3 days until 12 AM (12 in 24-hours format).
Do you guys know any idea why?
Upvotes: 3
Views: 4382
Reputation: 886
Just wanted to update this.
The beware the difference between HH and hh.
HH is 24 hours so the start of day is 12 where as HH the start is 0.
.format('HH:mm:ss') returns 00:00:00 .format('hh:mm:ss') returns 12:00:00
Upvotes: 6
Reputation: 6017
See (moment docs)[http://momentjs.com/docs/#/displaying/difference/].
Your example:
var moment = require('moment');
var a = moment("2014-05-02");
var b = moment();
var whole = a.diff(b, 'days');
var fract = a.diff(b, 'days', true);
console.log("Difference is " + whole + ", or more precisely, " + fract );
Returns:
Difference is 1, or more precisely, 1.404142025462963
Upvotes: 1