1

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

Bardelman
  • 2,176
  • 7
  • 43
  • 70
  • Please have a look at http://stackoverflow.com/questions/21580904/titanium-dosent-parse-date-as-expected/21594267#21594267. Your question look similar to the above link. – Anand Feb 17 '14 at 10:20
  • Thanks ! the split method in this function function FormatDate(date) { var arr = date.split(/[- :T]/); // from your example var date = "2012-11-14T06:57:36+0000"; //Ti.API.error("+*///+++ arr : "+arr); return new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], 00); } has resolved my problem and now i got both current date and the date gotten from the string in milliseconds but i coudn't compare them – Bardelman Feb 17 '14 at 11:05
  • i upvoted but accidently downvoted your comment,please paste that function as an answer and i will mark it as the correct answer – Bardelman Feb 17 '14 at 11:08

1 Answers1

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  
}
Anand
  • 5,323
  • 5
  • 44
  • 58