Skip to content

Instantly share code, notes, and snippets.

@captainpete
Created August 13, 2012 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save captainpete/3340841 to your computer and use it in GitHub Desktop.
Save captainpete/3340841 to your computer and use it in GitHub Desktop.
Date-based calendar formatter for momentjs
# Adapted from the momentjs calendar function
# Inspired by this answer: http://stackoverflow.com/a/10306813/325676
moment.fn.calendarTitle = ->
calendar =
sameDay : '[Today]',
nextDay : '[Tomorrow]',
nextWeek : 'dddd, Do MMM',
lastDay : '[Yesterday]',
lastWeek : '[Last] dddd',
sameElse : 'dddd, Do MMM'
diff = this.diff(moment().sod(), 'days', true)
format = if diff < -6 then calendar.sameElse else \
if diff < -1 then calendar.lastWeek else \
if diff < 0 then calendar.lastDay else \
if diff < 1 then calendar.sameDay else \
if diff < 2 then calendar.nextDay else \
if diff < 7 then calendar.nextWeek else \
calendar.sameElse
this.format(format)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment