JS:如何将此字符串转换为日期对象

时间:2012-04-13 14:29:18

标签: javascript date

字符串是

2012-04-13T22:59:33

我试过Date.parse(str, "Y-m-dTH:i:s")这对我不起作用,我不确定“T”代表什么?

1 个答案:

答案 0 :(得分:5)

只需将其作为参数添加到日期:

var date = new Date("2012-04-13T22:59:33");

如果这在旧浏览器中不起作用,请尝试:

var str = "2012-04-13T22:59:33",
    elems = str.split("T"),
    date = elems[0].split("-"),
    time = elems[1].split(":"),
    result = new Date(date[0],date[1]-1,date[2],time[0],time[1],time[2]);