2

I am using Appcelerator Titanium, and I'm trying to parse a date string as a new Date object and then use the .getTime() function but it keeps returning "NaN"

var d = new Date("2014-02-01T00:00:00");
var time = d.getTime();
console.log(time); // returns NaN

Am I doing anything wrong here? It works when I create a new date for now, like this:

var d = new Date();
var time = d.getTime();
console.log(time); // returns correct value

I can't see why the first example is working but the second example is not.

shrewdbeans
  • 11,971
  • 23
  • 69
  • 115
  • It doesn't appear that Date supports that functionality. Titanium uses JavaScript for the business logic and I can't find anything confirming that Date supports the way you are using it. It looks like there are attempts out there to try converting XML date http://stackoverflow.com/questions/8178598/xml-datetime-to-javascript-date-object, http://stackoverflow.com/questions/2731579/convert-an-xml-schema-date-string-to-a-javascript-date – Martin Feb 05 '14 at 20:31

1 Answers1

4

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 use Convert UTC Date to datetime string Titanium to convert the time.

Community
  • 1
  • 1
Anand
  • 5,323
  • 5
  • 44
  • 58