import { isDuration } from './constructor';
import {
getSetRelativeTimeRounding,
- getSetRelativeTimeThreshold
+ getSetRelativeTimeThreshold,
+ getSetRelativeTimeIncludeWeeks
} from './humanize';
export {
createDuration,
isDuration,
getSetRelativeTimeRounding,
- getSetRelativeTimeThreshold
+ getSetRelativeTimeThreshold,
+ getSetRelativeTimeIncludeWeeks
};
import { createDuration } from './create';
var round = Math.round;
+
var thresholds = {
ss: 44, // a few seconds to seconds
s : 45, // seconds to minute
m : 45, // minutes to hour
h : 22, // hours to day
- d : 26, // days to month
+ d : 26, // days to week
M : 11 // months to year
};
+var includeWeeks = false;
+
// helper function for moment.fn.from, moment.fn.fromNow, and moment.duration.fn.humanize
function substituteTimeAgo(string, number, withoutSuffix, isFuture, locale) {
return locale.relativeTime(number || 1, !!withoutSuffix, string, isFuture);
}
function relativeTime (posNegDuration, withoutSuffix, locale) {
- var duration = createDuration(posNegDuration).abs();
- var seconds = round(duration.as('s'));
- var minutes = round(duration.as('m'));
- var hours = round(duration.as('h'));
- var days = round(duration.as('d'));
- var months = round(duration.as('M'));
- var years = round(duration.as('y'));
+ var duration = createDuration(posNegDuration).abs();
+ var seconds = round(duration.as('s'));
+ var minutes = round(duration.as('m'));
+ var hours = round(duration.as('h'));
+ var days = round(duration.as('d'));
+ var weeks = round(duration.as('w'));
+ var months = round(duration.as('M'));
+ var years = round(duration.as('y'));
var a = seconds <= thresholds.ss && ['s', seconds] ||
seconds < thresholds.s && ['ss', seconds] ||
hours <= 1 && ['h'] ||
hours < thresholds.h && ['hh', hours] ||
days <= 1 && ['d'] ||
- days < thresholds.d && ['dd', days] ||
- months <= 1 && ['M'] ||
- months < thresholds.M && ['MM', months] ||
- years <= 1 && ['y'] || ['yy', years];
+ days < thresholds.d && ['dd', days];
+
+ if (includeWeeks) {
+ a = a || weeks <= 1 && ['w'] ||
+ weeks < thresholds.w && ['ww', weeks];
+ }
+ a = a || months <= 1 && ['M'] ||
+ months < thresholds.M && ['MM', months] ||
+ years <= 1 && ['y'] || ['yy', years];
a[2] = withoutSuffix;
a[3] = +posNegDuration > 0;
}
return true;
}
-
-export function humanize (withSuffix) {
+export function getSetRelativeTimeIncludeWeeks(setIncludeWeeks) {
+ if (setIncludeWeeks === undefined) {
+ return includeWeeks;
+ }
+ if (typeof setIncludeWeeks !== 'boolean') {
+ return false;
+ }
+ includeWeeks = setIncludeWeeks;
+ if (includeWeeks === true) {
+ thresholds.w = 4;
+ thresholds.d = 7;
+ }
+ if (includeWeeks === false) {
+ delete thresholds.w;
+ thresholds.d = 26;
+ }
+ return true;
+}
+export function humanize (withSuffix, includeWeeks) {
if (!this.isValid()) {
return this.localeData().invalidDate();
}
hh : '%d hours',
d : 'a day',
dd : '%d days',
+ w : 'a week',
+ ww : '%d weeks',
M : 'a month',
MM : '%d months',
y : 'a year',
isDuration,
createDuration as duration,
getSetRelativeTimeRounding as relativeTimeRounding,
- getSetRelativeTimeThreshold as relativeTimeThreshold
+ getSetRelativeTimeThreshold as relativeTimeThreshold,
+ getSetRelativeTimeIncludeWeeks as relativeTimeIncludeWeeks
} from './lib/duration/duration';
import { normalizeUnits } from './lib/units/units';
setHookCallback(local);
-moment.fn = fn;
-moment.min = min;
-moment.max = max;
-moment.now = now;
-moment.utc = utc;
-moment.unix = unix;
-moment.months = months;
-moment.isDate = isDate;
-moment.locale = locale;
-moment.invalid = invalid;
-moment.duration = duration;
-moment.isMoment = isMoment;
-moment.weekdays = weekdays;
-moment.parseZone = parseZone;
-moment.localeData = localeData;
-moment.isDuration = isDuration;
-moment.monthsShort = monthsShort;
-moment.weekdaysMin = weekdaysMin;
-moment.defineLocale = defineLocale;
-moment.updateLocale = updateLocale;
-moment.locales = locales;
-moment.weekdaysShort = weekdaysShort;
-moment.normalizeUnits = normalizeUnits;
-moment.relativeTimeRounding = relativeTimeRounding;
-moment.relativeTimeThreshold = relativeTimeThreshold;
-moment.calendarFormat = getCalendarFormat;
-moment.prototype = fn;
+moment.fn = fn;
+moment.min = min;
+moment.max = max;
+moment.now = now;
+moment.utc = utc;
+moment.unix = unix;
+moment.months = months;
+moment.isDate = isDate;
+moment.locale = locale;
+moment.invalid = invalid;
+moment.duration = duration;
+moment.isMoment = isMoment;
+moment.weekdays = weekdays;
+moment.parseZone = parseZone;
+moment.localeData = localeData;
+moment.isDuration = isDuration;
+moment.monthsShort = monthsShort;
+moment.weekdaysMin = weekdaysMin;
+moment.defineLocale = defineLocale;
+moment.updateLocale = updateLocale;
+moment.locales = locales;
+moment.weekdaysShort = weekdaysShort;
+moment.normalizeUnits = normalizeUnits;
+moment.relativeTimeRounding = relativeTimeRounding;
+moment.relativeTimeThreshold = relativeTimeThreshold;
+moment.relativeTimeIncludeWeeks = relativeTimeIncludeWeeks;
+moment.calendarFormat = getCalendarFormat;
+moment.prototype = fn;
// currently HTML5 input type only supports 24-hour formats
moment.HTML5_FMT = {
test('custom thresholds', function (assert) {
var a;
+ // including weeks
+ moment.relativeTimeIncludeWeeks(true);
+ // threshold for days to weeks with including weeks
+ a = moment();
+ a.subtract(6, 'days');
+ assert.equal(a.fromNow(), '6 days ago', 'Below threshold days for weeks');
+ a.subtract(1, 'days');
+ assert.equal(a.fromNow(), 'a week ago', 'Above threshold days for weeks');
+
+ // threshold for days to weeks with including weeks
+ a = moment();
+ a.subtract(3, 'weeks');
+ assert.equal(a.fromNow(), '3 weeks ago', 'Below threshold weeks for months');
+ a.subtract(1, 'week');
+ assert.equal(a.fromNow(), 'a month ago', 'Above threshold weeks for months');
+ moment.relativeTimeIncludeWeeks(false);
+ assert.equal(moment.relativeTimeIncludeWeeks(), false);
// Seconds to minute threshold, under 30
moment.relativeTimeThreshold('s', 25);