]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Allow custom formats to be passed to calendar function. 2424/head
authorJoe Freeman <joe.freeman@bitroot.com>
Wed, 17 Jun 2015 11:19:11 +0000 (12:19 +0100)
committerJoe Freeman <joe.freeman@bitroot.com>
Wed, 17 Jun 2015 11:19:11 +0000 (12:19 +0100)
src/lib/moment/calendar.js
src/test/moment/format.js

index 66d70adcc0b2ff4f862e3b2f5df5dc32e3b89c96..c9df8031123d3938581b3d2e314258013050e617 100644 (file)
@@ -1,7 +1,7 @@
 import { createLocal } from '../create/local';
 import { cloneWithOffset } from '../units/offset';
 
-export function calendar (time) {
+export function calendar (time, formats) {
     // We want to compare the start of today, vs this.
     // Getting start-of-today depends on whether we're local/utc/offset or not.
     var now = time || createLocal(),
@@ -13,5 +13,5 @@ export function calendar (time) {
             diff < 1 ? 'sameDay' :
             diff < 2 ? 'nextDay' :
             diff < 7 ? 'nextWeek' : 'sameElse';
-    return this.format(this.localeData().calendar(format, this, createLocal(now)));
+    return this.format(formats && formats[format] || this.localeData().calendar(format, this, createLocal(now)));
 }
index d6f442b1e541aee9cbb57b06d665ffe48b3bc49a..32ebddfc4ce878bf4a1edaa073f1888f29b8f903 100644 (file)
@@ -310,6 +310,12 @@ test('calendar day timezone', function (assert) {
     assert.equal(moment(c).local().calendar(d), 'Tomorrow at 11:59 PM', 'Tomorrow at 11:59 PM, not Yesterday, or the wrong time');
 });
 
+test('calendar with custom formats', function (assert) {
+    assert.equal(moment().calendar(null, {sameDay: '[Today]'}), 'Today', 'Today');
+    assert.equal(moment().add(1, 'days').calendar(null, {nextDay: '[Tomorrow]'}), 'Tomorrow', 'Tomorrow');
+    assert.equal(moment([1985, 1, 4]).calendar(null, {sameElse: 'YYYY-MM-DD'}), '1985-02-04', 'Else');
+});
+
 test('invalid', function (assert) {
     assert.equal(moment.invalid().format(), 'Invalid date');
     assert.equal(moment.invalid().format('YYYY-MM-DD'), 'Invalid date');