简体   繁体   中英

How to convert a date string to a Date object?

How to convert a date string to a Date object?

An example date string:

31.12.2009 23:12:00
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM