简体   繁体   中英

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.

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()); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM