新日期的日期无效(“YYYY-MM-DDThh:mm:ss”);实证 [英] Invalid Date for a new Date("YYYY-MM-DDThh:mm:ss"); instanciation

查看:157
本文介绍了新日期的日期无效(“YYYY-MM-DDThh:mm:ss”);实证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用钛作为基于javascript的移动开发框架。



我有一个数组,其中包含一个表示日期和时间''YYYY-MM-DD HH:mm:ss'格式的字符串(NightsArray [i] [3]返回:2014-02-20 23:00:00),如后面的控制台所示。



在此页面中,它显示了几个参数的Date()对象的构造函数:

  var today = new Date(); 
var birthday = new Date(1995年12月17日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);

所以你可以看到我的数组的字符串最接近的构造函数是第三个:

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

在下面的代码中,我将尝试格式化我的字符串在YYYY-MM-DDThh:mm :ss形式与一些substr()方法,并将生成的字符串(连接后)传递给Date()构造函数,但是我收到'无效的日期',如控制台日志中所示。

  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);

这是控制台日志:

  [ERROR]:+ * /// +++ NIGHT DATE& TIME(string):2014-02-19 23:00:00 
[错误]:+ * /// +++ 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)

我必须比较该字符串到最后的当前日期,但现在我首先必须转换字符串成为一个日期,但我想知道为什么这不工作..

你正在尝试解析一个UTC的日期时间。在Titanium中,当您尝试解析日期时,它将返回无效的日期。所以你需要将它转换成datetime字符串。您可以选择是将分隔符上的字符串分隔符 - 和和,并将每个结果的数组项传递给Date构造函数。



尝试以下

  function FormatDate(date)
{
var arr = date.split(/ [ - :T] ),//从你的例子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);
// ..进一步的东西在这里
}


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..

解决方案

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  
}

这篇关于新日期的日期无效(“YYYY-MM-DDThh:mm:ss”);实证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆