0

How to convert a date string to a Date object?

An example date string:

31.12.2009 23:12:00
1
  • 1
    The word you're looking for is "convert" Commented Oct 11, 2011 at 13:42

3 Answers 3

3
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.

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

Comments

2

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.