Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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

— asked December 16th 2011 by Katja Zimmermann
  • date
  • ipad
  • mobile
1 Comment
  • so of course it should be :

    Ti.App.Properties.setString('lastStateDate', JSON.stringify(new Date()));
    

    sorry for the mess

    — commented December 16th 2011 by Katja Zimmermann

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

    — answered December 16th 2011 by Ivan Škugor
    permalink
    4 Comments
    • sorry i screwed up the copy and pasting :) .. i 'll fix that .. but as already mentioned that is definitely not the problem .. :(

      — commented December 16th 2011 by Katja Zimmermann
    • Okay. Try to remove JSON.stringify, just call "new Date().toString()"

      — commented December 16th 2011 by Ivan Škugor
    • that works .. thanks :)

      — commented December 16th 2011 by Katja Zimmermann
    • a late ps .. as i just read it now :) .. the testing for all possible and unpossible things is just something i formed a habit of .. as sometimes titanium shows really weired behaviour (especially on device) .. and i have colleagues .. u never know what they do :D ..

      — commented December 16th 2011 by Katja Zimmermann
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.