Invalid Date for a new Date(“YYYY-MM-DDThh:mm:ss”); instanciation

倾然丶 夕夏残阳落幕 提交于 2019-12-20 03:59:21

问题


I'm working with titanium which is a framework for mobile developpement based on javascript.

I've an array which contains among its cells a string representing a 'date and time' string in the 'YYYY-MM-DD HH:mm:ss' format ( NightsArray[i][3] returns : 2014-02-20 23:00:00) as shown in the console later.

in this page it's shown several constructors for the Date() object with several parameters :

var today = new Date();
var birthday = new Date("December 17, 1995 03:24:00");
var birthday = new Date("1995-12-17T03:24:00");
var birthday = new Date(1995,11,17);
var birthday = new Date(1995,11,17,3,24,0);

So as you can see the closest constructor for my array's string is the 3th one :

var birthday = new Date("1995-12-17T03:24:00");

In the following code i will try to format my string in the "YYYY-MM-DDThh:mm:ss" form with some substr() methods and pass the resulting string (after a concatenation) to the Date() constructor but i got 'Invalid Date' as shown in the console log.

        Ti.API.error("+*///+++NIGHT DATE & TIME(string) : "+NightsArray[i][3]);
        Ti.API.error("+*///+++NIGHT DATE & TIME(string)substr(0,10) : "+NightsArray[i][3].substr(0,10));
        Ti.API.error("+*///+++NIGHT DATE & TIME(string)substr(11,10) : "+NightsArray[i][3].substr(11,10));

        Ti.API.error("+*///+++"+NightsArray[i][3].substr(0,10)+"T"+NightsArray[i][3].substr(11,10));

        var nightDateNTime =  new Date(NightsArray[i][3].substr(0,10)+"T"+NightsArray[i][3].substr(11,10));
        Ti.API.error("+*///+++ CURRENT DATE nightDateNTime(Date): "+nightDateNTime);
        var d = new Date();
        Ti.API.error("+*///+++ CURRENT DATE : "+d);

This is the console log :

[ERROR] :+*///+++NIGHT DATE & TIME(string) : 2014-02-19 23:00:00
[ERROR] :  +*///+++NIGHT DATE & TIME(string)substr(0,10) : 2014-02-19
[ERROR] :  +*///+++NIGHT DATE & TIME(string)substr(11,10) : 23:00:00
[ERROR] :  +*///+++2014-02-19T23:00:00
[ERROR] :  +*///+++ CURRENT DATE nightDateNTime(Date): Invalid Date
[ERROR] :  +*///+++ CURRENT DATE : Mon Feb 17 2014 11:09:09 GMT+0100 (CET)

i have to compare that string to the current date at the end but now i first have to convert that string into a date but i wonder why this doesn't work..


回答1:


You're trying to parse a UTC date time. In Titanium, when you try to parse the date, it will return invalid date. So you need to convert it to datetime string. You can choose is to split the string on the separator characters -, and : , and pass each of the resulting array items to the Date constructor.

Try the following

function FormatDate(date)
{   
    var arr = date.split(/[- :T]/), // from your example var date = "2012-11-14T06:57:36+0000";
    date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], 00);
    newDate = date.toString("MMMM");
    //.. do further stuff here  
}


来源:https://stackoverflow.com/questions/21826161/invalid-date-for-a-new-dateyyyy-mm-ddthhmmss-instanciation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!