Javascript Function/Method getTime() not working/returning Nan on parsed string
Hey there .. i'm not sure if it is a specific Titanium problem or if some experienced something similar in 'normal' javascript .. i didn't ..
basically i'm trying to calculate a time difference .. and
narrowed the problem down to:
the javascript 'getTime()' function returns NaN for a Date parsed from string ..
so somewhere in my app i do this
Ti.App.Properties.setString('lastStateDate') = JSON.stringify(new Date());
and on the next startup i'll check for the value
var v_string = Ti.App.Properties.getString('lastStateDate');
var now = new Date();
var last = new Date(v_string);
if(now instanceof Date && last instanceof Date){
//everything ok until here.
var diff = now.getTime() - last.getTime();
....
}
(i removed all the if and elses for testing for undefined and null values and stuff .. so thats not the problem .. meaning the value of 'v_string' is a valid datetime string .. even 'last' has the correct value)
So any help or hint is appreciated
Greetz Katja
PS: ipad, sdk 1.7.5, ios 5.0
1 Answer
-
Accepted Answer
Hi Katja.
You're setting app-property in a wrong way. "setString" takes two arguments, property name and property value. So, right call would be:
Ti.App.Properties.setString('lastStateDate', JSON.stringify(new Date()));
Also, if you create "Date" objects, they will be instances of "Date" for sure, so your check "if(now instanceof Date && last instanceof Date){" is useless here, you need to check for something else. ;)