Created
February 8, 2012 16:50
-
-
Save timrwood/1771023 to your computer and use it in GitHub Desktop.
Moment calendar without time plugin
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
(function(){ | |
var oldcal = moment.calendar; | |
var newcal = { | |
sameDay : '[Today]', | |
nextDay : '[Tomorrow]', | |
nextWeek : 'dddd', | |
lastDay : '[Yesterday]', | |
lastWeek : '[last] dddd', | |
sameElse : 'L' | |
}; | |
moment.fn.oldcalendar = moment.fn.calendar; | |
moment.fn.calendar = function(withoutTime) { | |
if (withoutTime) { | |
moment.calendar = newcal; | |
} else { | |
moment.calendar = oldcal; | |
} | |
return this.oldcalendar(); | |
} | |
})() |
It works perfectly.
(moment(time).calendar().split(" at"))[0]
Also
moment().calendar().substring(0, moment().calendar().indexOf(' at'))
More verbose, but doesn't create an unnecessary array
Also
moment().calendar().substring(0, moment().calendar().indexOf(' at'))
More verbose, but doesn't create an unnecessary array
this approach doesn't work and return empty string "", if moment doesn't contain " at" this is all "sameElse" dates.
default:
moment().calendar(null, {
sameDay: '[Today]',
nextDay: '[Tomorrow]',
nextWeek: 'dddd',
lastDay: '[Yesterday]',
lastWeek: '[Last] dddd',
sameElse: 'DD/MM/YYYY'
});
+1 to the
(moment(time).calendar().split(" at"))[0]
thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It no longer works with current moment.js
Had to update it this way: https://gist.github.com/anri82/10013485