admin管理员组

文章数量:1290941

I have data in mm-dd-yyyy format. I need it to pare 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.

I have data in mm-dd-yyyy format. I need it to pare 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.

Share Improve this question edited Nov 3, 2016 at 15:05 Zakaria Acharki 67.5k15 gold badges78 silver badges106 bronze badges asked Nov 3, 2016 at 14:53 Surendra MouryaSurendra Mourya 6113 gold badges10 silver badges33 bronze badges 6
  • 1 if you have no problem in using third party library use moment.js – Geeky Commented Nov 3, 2016 at 14:54
  • moment.js is wonderful library for date formatting and converstions – Geeky Commented Nov 3, 2016 at 14:54
  • 1 How does November and month==12 go together? – connexo Commented Nov 3, 2016 at 14:55
  • 1 Months are zero based in JavaScript – Scott Marcus Commented Nov 3, 2016 at 14:55
  • 1 If your result is July (7) 12th and you wanted December (12) 7th, what do you think could be a solution to that? – JJJ Commented Nov 3, 2016 at 14:57
 |  Show 1 more ment

1 Answer 1

Reset to default 10

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

本文标签: Convert mmddyyyy to date in javascriptStack Overflow