Fetching contributors…
Cannot retrieve contributors at this time
75 lines (65 sloc) 1.76 KB
title version signature
Calendar Time
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 per invocation:

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 be a callback that is executed within the moment context with a single argument now:

moment().calendar(null, {
  sameDay: function (now) {
    if (this.isBefore(now)) {
      return '[Will Happen Today]';
    } else {
      return '[Happened Today]';
    /* ... */