From f282ddad330fe963524f1053e4267dffc8b998a0 Mon Sep 17 00:00:00 2001 From: Joel Gillman Date: Wed, 26 Mar 2014 16:38:01 -0700 Subject: [PATCH] Add optional time argument to calendar method Mostly useful for running tests when you need to stub out time as a specific time. Or for if you want to get "calendar" style formating relative to a time that is not "now". --- moment.js | 5 +++-- test/moment/format.js | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/moment.js b/moment.js index 0018faebf..573830e69 100644 --- a/moment.js +++ b/moment.js @@ -1922,10 +1922,11 @@ return this.from(moment(), withoutSuffix); }, - calendar : function () { + calendar : function (time) { // We want to compare the start of today, vs this. // Getting start-of-today depends on whether we're zone'd or not. - var sod = makeAs(moment(), this).startOf('day'), + var now = time || moment(), + sod = makeAs(now, this).startOf('day'), diff = this.diff(sod, 'days', true), format = diff < -6 ? 'sameElse' : diff < -1 ? 'lastWeek' : diff --git a/test/moment/format.js b/test/moment/format.js index e855719e8..80e76a699 100644 --- a/test/moment/format.js +++ b/test/moment/format.js @@ -364,12 +364,13 @@ exports.format = { }, "calendar day timezone" : function (test) { - test.expect(10); + test.expect(11); moment.lang('en'); var zones = [60, -60, 90, -90, 360, -360, 720, -720], b = moment().utc().startOf('day').subtract({ m : 1 }), c = moment().local().startOf('day').subtract({ m : 1 }), + d = moment().local().startOf('day').subtract({ d : 2 }), i, z, a; for (i = 0; i < zones.length; ++i) { @@ -380,6 +381,7 @@ exports.format = { test.equal(moment(b).utc().calendar(), "Yesterday at 11:59 PM", "Yesterday at 11:59 PM, not Today, or the wrong time"); test.equal(moment(c).local().calendar(), "Yesterday at 11:59 PM", "Yesterday at 11:59 PM, not Today, or the wrong time"); + test.equal(moment(c).local().calendar(d), "Tomorrow at 11:59 PM", "Tomorrow at 11:59 PM, not Yesterday, or the wrong time"); test.done(); }, -- 2.47.2