From: Joel Gillman Date: Wed, 26 Mar 2014 23:38:01 +0000 (-0700) Subject: Add optional time argument to calendar method X-Git-Tag: 2.7.0~16^2 X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=refs%2Fpull%2F1554%2Fhead;p=thirdparty%2Fmoment.git 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". --- 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(); },