如何在Moment.js中格式化日期 [英] How to format a date in Moment.js
问题描述
我无法在Moment.js中正确格式化日期.我正在使用格式为"LLL D,YYYY"的格式函数,因此它应返回"2016年9月15日"之类的信息.
I'm having trouble formatting a date correctly in Moment.js. I'm using the format function with format "LLL D, YYYY" so it should return something like "Sep 15, 2016".
相反,它以一种奇怪的格式返回日期,例如"2016年9月15日,12:00 AM,2016年1月15日".
Instead, it's returning a the date in a weird format like "September 15, 2016 12:00 AM 15, 2016".
这是我的代码,下面是调试信息.
Here is my code, with the debugging info below.
moment.locale(picker.options.language);
console.log('picker.options.language:');
console.log(picker.options.language);
formatted = moment(picker.date).format(picker.format);
console.log('picker.date:');
console.log(picker.date);
console.log('picker.format:');
console.log(picker.format);
console.log('formatted:');
console.log(formatted);
以及上面代码的控制台输出:
And the console output from the above code:
推荐答案
来自 http://momentjs.com/docs/#/displaying/format/中,我们可以看到"LLL"表示格式为月名称,月日,年,时间".似乎您想要年月日",即"LL".
From http://momentjs.com/docs/#/displaying/format/ we can see that "LLL" represents the format "Month name, day of month, year, time". It seems you want "Month day, year", which is "LL".
尝试:
picker.format = 'LL';
formatted = moment(picker.date).format(picker.format);
console.log(formatted);
输出(带有今天的日期):
Outputs (with today's date):
September 15, 2016
这篇关于如何在Moment.js中格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!