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?
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With