Surendra Mourya Surendra Mourya - 1 month ago 5
Javascript Question

Convert mm-dd-yyyy to date in javascript

I have data in mm-dd-yyyy format. I need it to compare with current date in javascript.

I have got something for data, but it is not working.

My date is 12-07-2016 in mm-dd-yyyy format.

var d = '12-07-2016';
var date= d.split("-");
var f = new Date(date[2], date[1] - 1, date[0]);


Here I am getting date as :


Tue Jul 12 2016 00:00:00 GMT+0530 (India Standard Time)


But I need it as :


07 Dec 2016.


Thanks in advance.

Answer

You just have your indices the wrong way around for month and day...

var f = new Date(date[2], date[0] - 1, date[1]);

That will give you 07 DEC 2016

var d = '12-07-2016';
var date= d.split("-");
var f = new Date(date[2], date[0] - 1, date[1]);
console.log(f.toString());