sahira shamsu
sahira shamsu

Reputation: 47

can not format date properly when using date.js

Iam trying to convert the format of date from dd-MM-yyyy into yyyy-MM-dd using date.js java script libray .. for that I used following code

var myDate = Date.parse("15-09-2014");
DateFormat = myDate.toString('yyyy-MM-dd');
alert(DateFormat );

so when alerting DateFormat i got the correct output as 2014-09-15 . But when i am trying to format a date in which the day is below 13 , i got the output in which the day and date positions interchanged... for example when i give input as "12-09-2014", i got the result as 2014-12-09....but am expecting 2014-09-12... Can anybody help me please...?

Upvotes: 2

Views: 259

Answers (1)

TeeDeJee
TeeDeJee

Reputation: 3741

You can use parseExact to define your format.

var myDate = Date.parseExact("11-09-2014", "d-M-yyyy");
DateFormat = myDate.toString('yyyy-MM-dd');
alert(DateFormat);

See http://jsfiddle.net/mf4v6wjd/1/

Upvotes: 1

Related Questions