Calendar Time1.3.0+

  1. moment().calendar();
  2. moment().calendar(referenceTime);
  3. 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 weekLast Monday at 2:30 AM
The day beforeYesterday at 2:30 AM
The same dayToday at 2:30 AM
The next dayTomorrow at 2:30 AM
The next weekSunday at 2:30 AM
Everything else7/10/2011

These strings are localized, and can be customized.

From 2.10.5 moment supports specifying calendar output formats perinvocation:

  1. moment().calendar(null, {
  2. sameDay: '[Today]',
  3. nextDay: '[Tomorrow]',
  4. nextWeek: 'dddd',
  5. lastDay: '[Yesterday]',
  6. lastWeek: '[Last] dddd',
  7. sameElse: 'DD/MM/YYYY'
  8. });

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:

  1. moment().calendar(null, {
  2. sameDay: function (now) {
  3. if (this.isBefore(now)) {
  4. return '[Will Happen Today]';
  5. } else {
  6. return '[Happened Today]';
  7. }
  8. /* ... */
  9. }
  10. });