locked
Date.parse returns NaN

    Question

  • I have a variable str with a value of "9/6/2013 8:05 AM".

    When I use Date.parse(str) it returns NaN.

    How can I get this into a date?  I need to turn it into a javascript timestamp to be used as seen

    http://people.iola.dk/olau/flot/examples/time.html

    Also, when I type Date.parse("9/6/2013 8:05 AM") in the immediate window it works.

    Ive even tried Date.parse(String(str))


    Writing code is fun



    • Edited by attrib75 Friday, September 6, 2013 4:40 PM
    Friday, September 6, 2013 4:16 PM

Answers


  • @Jeff yes, that is correct.  I figured out a work around.  I'm just recording the javascript time stamp of new Date() at the time when I need to save the "date played" data.  When Its time to get the stored data, I just read the timestamp instead of trying to read the stored date string into a date and make a timestamp out of it. 

    Writing code is fun

    Monday, September 9, 2013 8:53 PM

All replies

  • This works great for me!

        var theStr = "9/6/2013 8:05 AM";
                    var theDate = Date.parse(theStr);


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Friday, September 6, 2013 7:56 PM
    Moderator
  • Hello attrib75,

    Try this:

    var str = "9/6/2013 8:05 AM";
            var dta = new Date(str).toLocaleDateString();

    In this code you can change toLocaleDateSttring to another type which is suitable for You.

    Saturday, September 7, 2013 10:52 AM
  • Thanks, but this returns "Invalid Date"

    Maybe its because I'm looping through an array to extract the date value.  Either way, it works when I paste the string value into new Date(<paste>) in the immediate window, but not in the code.  Not sure what is going on.  The array value is a string

       var h = [];
                var hist = getGameHistory();
                for (var i = 0; i < hist.length; i++) {
                    var histi = hist[i];
                    var str = histi.datePlayed
                                              
                    var date = new Date(str);
                    
                    h.push([date.getTime(), histi.score]);
                }


    Writing code is fun

    Saturday, September 7, 2013 12:46 PM
  • So wait,

    You took my exact code and pasted it into an app and got different results?


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Monday, September 9, 2013 12:23 PM
    Moderator

  • @Jeff yes, that is correct.  I figured out a work around.  I'm just recording the javascript time stamp of new Date() at the time when I need to save the "date played" data.  When Its time to get the stored data, I just read the timestamp instead of trying to read the stored date string into a date and make a timestamp out of it. 

    Writing code is fun

    Monday, September 9, 2013 8:53 PM
  • Ive been having the very same issue and i found that the text had special formatting chars in it. try doing a replace on the string and removing all non standard chars first.

    .replace(/[^0-9//]/g,"") worked for me.I seemed to have issues with something adding LTR char codes into my strings which then means you can convert them to int or date or anything else.

    Tuesday, January 21, 2014 8:42 AM