Javascript: String to date not working

I want to convert the date specified as yyyy-mm-ddThh: mm: ss to a Javascript date.

First step:

var str = "2013-10-31T18:15:30";
var date = new Date(str)

Returns Thu Oct 31 2013 18:15:30 GMT + 0100 (CET) .

Second attemp:

var str = "2013-10-31T18:15:30";
var str_parts = str.split("T");
var date_parts = str_parts[0].split("-");
var time_parts = str_parts[1].split(":");
var date = new Date(date_parts[0], date_parts[1], date_parts[2], time_parts[0], time_parts[1], time_parts[2]);

Returns Sun Dec 01 2013 18:15:30 GMT + 0100 (CET) . Did I miss something? Should not also come back Thu October 31, 2013 18:15:30 GMT + 0100 (CET) ? One way or another, the date is incorrect, and the time is right.

Corresponding script: http://jsfiddle.net/4CLAj/2/

+4
source share
1 answer

Date (, , , , , ) , .. .

, :

var date = new Date(date_parts[0], Number(date_parts[1]) - 1, date_parts[2], time_parts[0], time_parts[1], time_parts[2]);

. http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf 15.9.1.4

0

All Articles