Created
August 13, 2012 13:36
-
-
Save captainpete/3340841 to your computer and use it in GitHub Desktop.
Date-based calendar formatter for momentjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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