Calendar Time1.3.0+
moment().calendar();
moment().calendar(referenceTime);
moment().calendar(referenceTime, formats); // from 2.10.5
Calendar time displays time relative to a given referenceTime
(defaults to now), but does so slightly differently than moment#fromNow
.
moment#calendar
will format a date with different strings depending on how close to referenceTime
's date (today by default) the date is.
Last week | Last Monday at 2:30 AM |
The day before | Yesterday at 2:30 AM |
The same day | Today at 2:30 AM |
The next day | Tomorrow at 2:30 AM |
The next week | Sunday at 2:30 AM |
Everything else | 7/10/2011 |
These strings are localized, and can be customized.
From 2.10.5 moment supports specifying calendar output formats perinvocation:
moment().calendar(null, {
sameDay: '[Today]',
nextDay: '[Tomorrow]',
nextWeek: 'dddd',
lastDay: '[Yesterday]',
lastWeek: '[Last] dddd',
sameElse: 'DD/MM/YYYY'
});
sameElse
is used as the format when the moment is more than a week away from the referenceTime
Note: From version 2.14.0 the formats argument to calendar can bea callback that is executed within the moment context with a single argumentnow:
moment().calendar(null, {
sameDay: function (now) {
if (this.isBefore(now)) {
return '[Will Happen Today]';
} else {
return '[Happened Today]';
}
/* ... */
}
});
当前内容版权归 momentjs.com 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 momentjs.com .