0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

@Nic if you're using this, there was a bug in there because JavaScript counts months from 0 to 11, so month 4 is interpreted as May where it should be April. I fixed it in my answer after you accepted it by subtracting 1 from the found part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.