How to get duration in weeks using Moment.js?

I use moment.js to format the duration in humanoid format.
For example ( d is a Date object):

  moment (d) .subtract ("days", 3) .from (d) // returns "3 days ago" 

Now I would like to get "2 weeks ago", but the code below returns the duration in days

  moment (d) .subtract ("weeks", 2) .from (d) // returns "14 days ago" i / o "2 weeks ago" 

How can I get "2 weeks ago" with moment.js ?

+7
source share
6 answers

You can do this quite easily with a special callback function.

 moment.relativeTime.dd = function (number) { // round to the closest number of weeks var weeks = Math.round(number / 7); if (number < 7) { // if less than a week, use days return number + " days"; } else { // pluralize weeks return weeks + " week" + (weeks === 1 ? "" : "s"); } } 

http://jsfiddle.net/timrwood/sWsXQ/

+8
source

Use the new updateLocale() .

Moment.js is updated again, so the preferred method is :

 moment.updateLocale('en', { relativeTime : { future: 'in %s', past: '%s ago', s: 'a few seconds', m: 'a minute', mm: '%d minutes', h: 'an hour', hh: '%d hours', d: 'a day', dd: function(number) { if (number < 7) { return number + ' days'; // Moment uses "d" when it just 1 day. } else { var weeks = Math.round(number / 7); return weeks + ' ' + (weeks > 1 ? 'weeks' : 'week'); } }, M: 'a month', MM: '%d months', y: 'a year', yy: '%d years' } }); 

Thanks to @timrwood and @gahen for the answers.

I am working on updating Moment so that you can only redefine a single relativeTime object, such as dd , instead of providing all the objects.

The GitHub problem is here , so give it a thumbs up if you want to do something like this:

 moment.updateLocale('en', { relativeTime : { dd: function(number) { if (number < 7) { return number + ' days'; // Moment uses "d" when it just 1 day. } else { var weeks = Math.round(number / 7); return weeks + ' ' + (weeks > 1 ? 'weeks' : 'week'); } } } }); 
+3
source

I can not comment on the previous answer, but I wanted to leave an updated answer (based on timrwood )

 moment.locale('en', { relativeTime : { future: "in %s", past: "%s ago", s: "seconds", m: "a minute", mm: "%d minutes", h: "an hour", hh: "%d hours", d: "a day", dd: function (number) { var weeks = Math.round(number / 7); if (number < 7) { // if less than a week, use days return number + " days"; } else { // pluralize weeks return weeks + " week" + (weeks === 1 ? "" : "s"); } }, M: "a month", MM: "%d months", y: "a year", yy: "%d years" } }); $window.moment.relativeTimeThreshold('d', 340); // if you want weeks instead of months, otherwise put a 28 or so. 
+2
source

exchange and hope are still useful:

 var aPastDate = moment().subtract(5,'months'); var now = moment(); moment.fn.durationInWeeks = function (fromDate, toDate) { var weeks = toDate.diff( fromDate, 'weeks' ); return weeks + ' weeks ago'; //your own format } moment().durationInWeeks(aPastDate,now); //"21 weeks ago" 
+1
source

improvement on @vinjenzo's answer, when you need the date of the difference in weeks, you are likely to come across the returned days:

 var aPastDate = moment().subtract(5,'months'); var aPastDay = moment().subtract(6,'days'); var now = moment(); moment.fn.durationInWeeks = function(fromDate, toDate) { var days = toDate.diff(fromDate, 'days'); var weeks = toDate.diff(fromDate, 'weeks'); if (weeks === 0) { return days + ' ' + (days > 1 ? 'days' : 'day'); } else { return weeks + ' ' + (Math.round(days / 7) > 1 ? 'weeks' : 'week'); } } moment().durationInWeeks(aPastDate,now); // 21 weeks moment().durationInWeeks(aPastDay,now); // 6 days 
0
source

All Articles