import isFunction from '../utils/is-function';
export function calendar (key, mom, now) {
- var output = this._calendar[key];
+ var output = this._calendar[key] || this._calendar['sameElse'];
return isFunction(output) ? output.call(mom, now) : output;
}
import { createLocal } from '../create/local';
import { cloneWithOffset } from '../units/offset';
import isFunction from '../utils/is-function';
+import { hooks } from '../utils/hooks';
-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(),
- sod = cloneWithOffset(now, this).startOf('day'),
- diff = this.diff(sod, 'days', true),
- format = diff < -6 ? 'sameElse' :
+export function getCalendarFormat(myMoment, now) {
+ var diff = myMoment.diff(now, 'days', true);
+ return diff < -6 ? 'sameElse' :
diff < -1 ? 'lastWeek' :
diff < 0 ? 'lastDay' :
diff < 1 ? 'sameDay' :
diff < 2 ? 'nextDay' :
diff < 7 ? 'nextWeek' : 'sameElse';
+}
+
+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(),
+ sod = cloneWithOffset(now, this).startOf('day'),
+ format = hooks.calendarFormat(this, sod) || 'sameElse';
var output = formats && (isFunction(formats[format]) ? formats[format].call(this, now) : formats[format]);
var proto = Moment.prototype;
import { add, subtract } from './add-subtract';
-import { calendar } from './calendar';
+import { calendar, getCalendarFormat } from './calendar';
import { clone } from './clone';
import { isBefore, isBetween, isSame, isAfter, isSameOrAfter, isSameOrBefore } from './compare';
import { diff } from './diff';
proto.add = add;
proto.calendar = calendar;
+proto.calendarFormat = getCalendarFormat;
proto.clone = clone;
proto.diff = diff;
proto.endOf = endOf;
createInZone as parseZone
} from './lib/moment/moment';
+import {
+ getCalendarFormat
+} from './lib/moment/calendar';
+
import {
defineLocale,
updateLocale,
moment.normalizeUnits = normalizeUnits;
moment.relativeTimeRounding = relativeTimeRounding;
moment.relativeTimeThreshold = relativeTimeThreshold;
+moment.calendarFormat = getCalendarFormat;
moment.prototype = fn;
export default moment;
}), '1:00PM', 'should equate');
});
+test('extending calendar options', function (assert) {
+ var calendarFormat = moment.calendarFormat;
+ moment.calendarFormat = function (myMoment, now) {
+ var diff = myMoment.diff(now, 'days', true);
+ var nextMonth = now.clone().add(1, 'month');
+
+ var retVal = diff < -6 ? 'sameElse' :
+ diff < -1 ? 'lastWeek' :
+ diff < 0 ? 'lastDay' :
+ diff < 1 ? 'sameDay' :
+ diff < 2 ? 'nextDay' :
+ diff < 7 ? 'nextWeek' :
+ (myMoment.month() === now.month() && myMoment.year() === now.year()) ? 'thisMonth' :
+ (nextMonth.month() === myMoment.month() && nextMonth.year() === myMoment.year()) ? 'nextMonth' : 'sameElse';
+ return retVal;
+ };
+
+ moment.updateLocale('en', {
+ calendar : {
+ sameDay : '[Today at] LT',
+ nextDay : '[Tomorrow at] LT',
+ nextWeek : 'dddd [at] LT',
+ lastDay : '[Yesterday at] LT',
+ lastWeek : '[Last] dddd [at] LT',
+ thisMonth : '[This month on the] Do',
+ nextMonth : '[Next month on the] Do',
+ sameElse : 'L'
+ }
+ });
+ var a = moment('2016-01-01').add(28, 'days');
+ var b = moment('2016-01-01').add(1, 'month');
+ try {
+ assert.equal(a.calendar('2016-01-01'), 'This month on the 29th', 'Ad hoc calendar format for this month');
+ assert.equal(b.calendar('2016-01-01'), 'Next month on the 1st', 'Ad hoc calendar format for next month');
+ assert.equal(a.locale('fr').calendar('2016-01-01'), a.locale('fr').format('L'), 'French falls back to default because thisMonth is not defined in that locale');
+ } finally {
+ moment.calendarFormat = calendarFormat;
+ moment.updateLocale('en', null);
+ }
+});