Calendar Format2.14.0+

  1. moment.calendarFormat = Function

This lets you modify the tokens used by calendar.

  1. moment.calendarFormat = function (myMoment, now) {
  2. var diff = myMoment.diff(now, 'days', true);
  3. var nextMonth = now.clone().add(1, 'month');
  4. var retVal = diff < -6 ? 'sameElse' :
  5. diff < -1 ? 'lastWeek' :
  6. diff < 0 ? 'lastDay' :
  7. diff < 1 ? 'sameDay' :
  8. diff < 2 ? 'nextDay' :
  9. diff < 7 ? 'nextWeek' :
  10. // introduce thisMonth and nextMonth
  11. (myMoment.month() === now.month() && myMoment.year() === now.year()) ? 'thisMonth' :
  12. (nextMonth.month() === myMoment.month() && nextMonth.year() === myMoment.year()) ? 'nextMonth' : 'sameElse';
  13. return retVal;
  14. };