javascript转换日期字符串为Date对象


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /wmcuit/data/www/wmcuit.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /wmcuit/data/www/wmcuit.com/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

如何把一个字符串格式为“YYYY-MM-DD HH:MM:SS”日期转换成标准的javascript中的Date对象,baidu后找到一个好的方法:http://swingboat.javaeye.com/blog/445618,整理如下。

 

把一个日期字符串如“2007-2-28 10:18:30”转换为Date对象:

 

1:

1
2
3
4
var strArray = str.split(" ");   
var strDate = strArray[0].split("-");   
var strTime = strArray[1].split(":");   
var a = new Date(strDate[0],(strDate[1]-parseInt(1)),strDate[2],strTime[0],strTime[1],strTime[2])

 

2:

1
2
var s = "2005-12-15 09:41:30";   
var d = new Date(Date.parse(s.replace(/-/g,"/")));