Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment.js issue format method is not working Correctly

var a = moment("24 12 1995").format('DD MM YYYY');
alert(a)

// This should be valid but its not.

var a = moment("12 24 1995").format('DD MM YYYY');
alert(a) 

// This should be Invalid, but its valid. (Month is 24)

Version : Moment.js 2.10.3

like image 806
Subscriber Avatar asked Sep 17 '25 08:09

Subscriber


2 Answers

You should pass the format as an argument:

moment("24 12 1995", "DD MM YYYY");

What .format function does is formatting the output.

So you can do:

var format = "DD MM YYYY";
var date = moment("24 12 1995", format);
alert(date.format(format));
like image 152
andrusieczko Avatar answered Sep 19 '25 04:09

andrusieczko


You could use the second parameter

moment("24 12 1995","DD MM YYYY");

to specify the format of the input string.

Then you can format it any way you want :

moment("24 12 1995","DD MM YYYY").format('MM DD YYYY')
moment("24 12 1995","DD MM YYYY").format('DD MM YYYY')
moment("24 12 1995","DD MM YYYY").format('ddd M YYYY')
like image 34
nik Avatar answered Sep 19 '25 04:09

nik