I was trying to use calendar time to format my dates by passing a callback function. However, in the else condition of my callback, the month of the formatted date is incorrect.
Jan
returned as Jamn
Mar
returned as 3amr
Feb
returned as F0b
I might be doing this wrong but it seems weird that the month would come back this way without throwing any errors.
Code to reproduce
export const dateConversion = (date) => {
// Assuming date comes in 'YYYY-MM-DD` format
date = moment(date, 'YYYY-MM-DD');
return date.calendar(null, {
sameDay: '[Today]',
nextDay: '[Tomorrow]',
lastDay: '[Yesterday]',
nextWeek: 'ddd',
lastWeek: 'MMM D',
sameElse: function(now) {
if (this.isSame(now, 'year')) {
return this.format('MMM D');
} else {
// Month is incorrectly parsed here
return this.format('MMM D, YYYY');
}
}
});
}
Environment
Tue Mar 28 2017 16:52:21 GMT-0400 (EDT)
3/28/2017, 4:52:21 PM
240
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
2.17.1
nevermind - was returning the wrong value.
@dbshwang i'm having the same issue. can you please put your resolution
@avi-meslati-sp You should only be returning the string for formatting - not the actual formatted date. So in my case:
sameElse: function(now) {
if (this.isSame(now, 'year')) {
return this.format('MMM D');
} else {
return this.format('MMM D, YYYY');
}
}
should actually be
sameElse: function(now) {
if (this.isSame(now, 'year')) {
return 'MMM D';
} else {
return 'MMM D, YYYY';
}
}
Most helpful comment
nevermind - was returning the wrong value.