Nic
Nic

Reputation: 437

Convert Date String to Date Object Javascript

How to convert this date string 19/04/2015:21:43:47.40 to Date object. new Date('19/04/2015:21:43:47.40') returns invalid date.

Upvotes: 0

Views: 80

Answers (1)

asontu
asontu

Reputation: 4639

To be absolutely sure I would split the string on any characters that aren't digits with a regex \D+. Then you have an array with all the parts and you can pass it into new Date() in the correct order:

var aParts = '19/04/2015:21:43:47.40'.split(/\D+/);
document.write(new Date(aParts[2], parseInt(aParts[1], 10)-1, aParts[0], aParts[3], aParts[4], aParts[5], aParts[6]));

Upvotes: 1

Related Questions