]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Add optional time argument to calendar method 1554/head
authorJoel Gillman <joeltgillman@gmail.com>
Wed, 26 Mar 2014 23:38:01 +0000 (16:38 -0700)
committerJoel Gillman <joeltgillman@gmail.com>
Wed, 26 Mar 2014 23:38:01 +0000 (16:38 -0700)
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
test/moment/format.js

index 0018faebf9fc17fe0486978732e7e5a4ed5197ce..573830e691199f2b3b707b8f2d38a186ca2eeb68 100644 (file)
--- a/moment.js
+++ b/moment.js
             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' :
index e855719e835ffb58384f1f20d9204ae1ee317923..80e76a699513ab48ece6a82b307dfd3a44162d4a 100644 (file)
@@ -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();
     },