How to convert a date string to a Date
object?
An example date string:
31.12.2009 23:12:00
开发者_StackOverflow社区
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.
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.
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.
精彩评论