我试图通过传递回调函数来使用日历时间来格式化我的日期。 但是,在我回调的 else 条件下,格式化日期的月份不正确。
Jan
返回为Jamn
Mar
返回为3amr
Feb
返回为F0b
我可能做错了,但这个月会以这种方式返回而不会抛出任何错误似乎很奇怪。
重现代码
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');
}
}
});
}
环境
2017 年 3 月 28 日星期二 16:52:21 GMT-0400 (EDT)
2017/3/28,下午 4:52:21
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
没关系 - 返回错误的值。
@dbshwang我遇到了同样的问题。 你能说出你的决议吗
@avi-meslati-sp 您应该只返回用于格式化的字符串 - 而不是实际的格式化日期。 所以在我的情况下:
sameElse: function(now) {
if (this.isSame(now, 'year')) {
return this.format('MMM D');
} else {
return this.format('MMM D, YYYY');
}
}
实际上应该是
sameElse: function(now) {
if (this.isSame(now, 'year')) {
return 'MMM D';
} else {
return 'MMM D, YYYY';
}
}
最有用的评论
没关系 - 返回错误的值。