How to format a date in Moment.js

空扰寡人 提交于 2019-12-12 01:38:23

问题


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".

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:


回答1:


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".

Try:

picker.format = 'LL';
formatted = moment(picker.date).format(picker.format);
console.log(formatted);

Outputs (with today's date):

September 15, 2016



回答2:


This should work...

formatted = moment(picker.date).format('MMM D, YYYY')

Ref: http://momentjs.com/docs/#/parsing/string-format/



来源:https://stackoverflow.com/questions/39516903/how-to-format-a-date-in-moment-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!