javascript - How to format a date in Moment.js - Stack Overflow

I'm having trouble formatting a date correctly in Moment.js. I'm using the format function wi

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:

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:

Share Improve this question asked Sep 15, 2016 at 17:20 amacrobertamacrobert 3,1793 gold badges30 silver badges46 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

This should work...

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

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

From http://momentjs./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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742354679a4428195.html

相关推荐

  • javascript - How to format a date in Moment.js - Stack Overflow

    I'm having trouble formatting a date correctly in Moment.js. I'm using the format function wi

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信