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

javascript - moment js date library, formatting on IE gives a NaN - Stack Overflow

programmeradmin1浏览0评论

im using moment js date library to format a date, but on IE i get a NaN on the output. It works fine on other browsers, like Chrome, FF, etc.

var value = "2015-11";

moment(value).format("YYYY-DD-01 00:00")    
> "0NaN-NaN-01 00:00"   

I was able to fix it by adding the same pattern on moment constructor like below:

> moment(value,"YYYY-DD-01 00:00").format("YYYY-DD-01 00:00")   
"2015-11-01 00:00"  

Is it a good practice to add this pattern on the constructor, for all moment objects creation so it can work also on IE?

im using moment js date library to format a date, but on IE i get a NaN on the output. It works fine on other browsers, like Chrome, FF, etc.

var value = "2015-11";

moment(value).format("YYYY-DD-01 00:00")    
> "0NaN-NaN-01 00:00"   

I was able to fix it by adding the same pattern on moment constructor like below:

> moment(value,"YYYY-DD-01 00:00").format("YYYY-DD-01 00:00")   
"2015-11-01 00:00"  

Is it a good practice to add this pattern on the constructor, for all moment objects creation so it can work also on IE?

Share Improve this question asked Jul 25, 2013 at 7:35 dotmindlabsdotmindlabs 9083 gold badges13 silver badges35 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

The input format should match what you are providing:

var value = "2015-11";
moment(value, "YYYY-MM")

If you want to format it differently for output, that's when you use the .format method.

var value = "2015-11";
var m = moment(value, "YYYY-MM")
var s = m.format("YYYY-MM-DD HH:MM")

Note that you were specifying DD which is the day formatter. But based on the usage, I think you meant MM for month.

发布评论

评论列表(0)

  1. 暂无评论