最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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
发布评论

评论列表(0)

  1. 暂无评论