Question

How to convert a date string to a Date object?

An example date string:

31.12.2009 23:12:00
Was it helpful?

Solution

I recommend the Date.js library.

It can handle all kinds of date parsing and converting, and other date related functionality. Very handy for this kind of thing.

hope that helps.

OTHER TIPS

var parts = "31.12.2009 23:12:00".match(/\d+/g);
new Date(parts[2], parts[1]-1, parts[0], parts[3], parts[4], parts[5]);

Parse it and create it.

Note: Month is zero based.

JavaScript by default parses date strings with the ISO 8601 format, which is...

YYYY-MM-DDTHH:mm:ss.sssZ

If you can get your datetime in this format it would probably be best. You do not want to run into any culture issues. In JavaScript you can do it with toISOString(). If you can't do this, you'll have to parse out the date yourself or use a library.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top