Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moment js date library, formatting on IE gives a NaN

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?

like image 299
dotmindlabs Avatar asked Jul 25 '13 07:07

dotmindlabs


People also ask

How do I set the date format in moments?

moment(). format('YYYY-MM-DD'); Calling moment() gives us the current date and time, while format() converts it to the specified format. This example formats a date as a four-digit year, followed by a hyphen, followed by a two-digit month, another hyphen, and a two-digit day.

Is MomentJS deprecated?

js . This library helps you manipulate, validate, and accurately display the date and time according to… In fact moment. js is deprecated, the development team recommends to use date-fns instead.

What can I use instead of moment in JavaScript?

And today, the most popular alternative to Moment. js is Day. js which surpassed date-fns on GitHub by stars (46,2k Day. js stars vs 37,7k date-fns stars).


1 Answers

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.

like image 75
Matt Johnson-Pint Avatar answered Sep 24 '22 07:09

Matt Johnson-Pint