Question

I'm using moment.js to format dates on my website. I'm displaying relative dates (6 hours ago, 2 months ago) instead of absolute dates (16 October 2013).

Instead of using months (2 months ago, 7 months ago), my aim is to display days (60 days ago, 210 days ago etc).

I've looked through moment.js and suspect I should modify diff : but I'm not sure what to change or if this is the right place. See here:

https://github.com/moment/moment/blob/develop/moment.js#L1997

Hoping for some help on how can I modify moment.js to display days instead of months by default?

Était-ce utile?

La solution

You can use .diff() like this:

moment().diff('2014-05-10', 'days'); // 3

You can extend moment to get a diffInDays function if you'd like:

moment.fn.diffInDays = function(m) {
  return this.diff(m, 'days');
}

Then you can do like:

moment().diffInDays(moment('2014-05-10'));

or even just:

moment().diffInDays('2014-05-10');
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top