I would like to use the moment.calendar() option without the time... so instead of "Last Tuesday at 5pm" I want "Last Tuesday". Does anybody know whether moment has a solution for that by now? I found this fiddle http://jsfiddle.net/nawxZ/, which apparently shows a solution for that, but I can't see how this is supposed to work? thanks carl
function log(str) { $('body').append('<p>' + str + '</p>'); } log(moment().calendar()); log(moment().calendar(true));
We can remove the time portion from a date with the startOf method. We pass in a date with the hour and minutes set into the moment function. Then we call startOf with the 'day' argument to get return a moment object with the time set to midnight of the same date.
moment(). calendar({ sameDay: '[Today]', nextDay: '[Tomorrow]', nextWeek: 'dddd', lastDay: '[Yesterday]', lastWeek: '[Last] dddd', sameElse: 'DD/MM/YYYY' }); moment(). calendar({ sameDay: function (now) { if (this. isBefore(now)) { return '[Will Happen Today]'; } else { return '[Happened Today]'; } /* ...
To get current date with Moment and JavaScript, we use the moment function without any arguments. const datetime = moment(); to call moment to get a moment object with the current date.
Moment construction falls back to js Date. This is discouraged and will be removed in an upcoming major release. This deprecation warning is thrown when no known format is found for a date passed into the string constructor.
starting from moment 2.10.5 you can do:
moment(/*your date*/).calendar(null,{ lastDay : '[Yesterday]', sameDay : '[Today]', nextDay : '[Tomorrow]', lastWeek : '[last] dddd', nextWeek : 'dddd', sameElse : 'L' })
see: http://momentjs.com/docs/#/displaying/calendar-time/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With