// format function strings
formatFunctions = {},
+ // default relative time thresholds
+ relativeTimeThresholds = {
+ s: 45, //seconds to minutes
+ m: 45, //minutes to hours
+ h: 22, //hours to days
+ dd: 25, //days to month (month == 1)
+ dm: 45, //days to months (months > 1)
+ dy: 345 //days to year
+ },
+
// tokens to ordinalize and pad
ordinalizeTokens = 'DDD w W M D d'.split(' '),
paddedTokens = 'M D H h m s w W'.split(' '),
hours = round(minutes / 60),
days = round(hours / 24),
years = round(days / 365),
- args = seconds < 45 && ['s', seconds] ||
+ args = seconds < relativeTimeThresholds.s && ['s', seconds] ||
minutes === 1 && ['m'] ||
- minutes < 45 && ['mm', minutes] ||
+ minutes < relativeTimeThresholds.m && ['mm', minutes] ||
hours === 1 && ['h'] ||
- hours < 22 && ['hh', hours] ||
+ hours < relativeTimeThresholds.h && ['hh', hours] ||
days === 1 && ['d'] ||
- days <= 25 && ['dd', days] ||
- days <= 45 && ['M'] ||
- days < 345 && ['MM', round(days / 30)] ||
+ days <= relativeTimeThresholds.dd && ['dd', days] ||
+ days <= relativeTimeThresholds.dm && ['M'] ||
+ days < relativeTimeThresholds.dy && ['MM', round(days / 30)] ||
years === 1 && ['y'] || ['yy', years];
args[2] = withoutSuffix;
args[3] = milliseconds > 0;
// It is intended to keep the offset in sync with the timezone.
moment.updateOffset = function () {};
+ // This function allows you to set a threshold for relative time strings
+ moment.relativeTimeThreshold = function(threshold, limit) {
+ if (relativeTimeThresholds[threshold] === undefined) {
+ return false;
+ }
+ relativeTimeThresholds[threshold] = limit;
+ return true;
+ };
+
// This function will load languages and then set the global language. If
// no arguments are passed in, it will simply return the current global
// language key.
--- /dev/null
+var moment = require("../../moment");
+
+exports.relativeTime = {
+ setUp : function (done) {
+ done();
+ },
+
+ "default thresholds" : function (test) {
+ test.expect(5);
+
+ var a = moment();
+ a.subtract('seconds', 10);
+ test.equal(a.fromNow(), "a few seconds ago", "Seconds friendly relative time");
+ a.subtract('seconds', 35);
+ test.equal(a.fromNow(), "a minute ago", "Minute friendly relative time");
+ a.subtract('minutes', 1);
+ test.equal(a.fromNow(), "2 minutes ago", "Minutes friendly relative time");
+ a.subtract('minutes', 45);
+ test.equal(a.fromNow(), "an hour ago", "1 hour friendly relative time");
+ a.subtract('hours', 22);
+ test.equal(a.fromNow(), "a day ago", "1 day friendly relative time");
+ test.done();
+ },
+
+ "custom thresholds" : function (test) {
+ test.expect(3);
+
+ var a = moment();
+ moment.relativeTimeThreshold('s', 55);
+ a.subtract('seconds', 54);
+ test.equal(a.fromNow(), "a few seconds ago", "Custom <1 minute friendly relative time");
+ moment.relativeTimeThreshold('s', 45);
+
+ a = moment();
+ moment.relativeTimeThreshold('m', 55);
+ a.subtract('minutes', 54);
+ test.equal(a.fromNow(), "54 minutes ago", "Custom <1 hour friendly relative time");
+ moment.relativeTimeThreshold('m', 45);
+
+ a = moment();
+ moment.relativeTimeThreshold('h', 24);
+ a.subtract('hours', 23);
+ test.equal(a.fromNow(), "23 hours ago", "Custom <1 day friendly relative time");
+ moment.relativeTimeThreshold('h', 22);
+ test.done();
+ }
+};