开发者

How to convert a date string to a Date object?

开发者 https://www.devze.com 2023-04-12 10:37 出处:网络
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);

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消