Search code examples
javascriptdatetimetimestamputc

Convert yyyy-mm-dd to JS timestamp


Theres lots of questions about how to convert a timestamp to yyyy-mm-dd but I am trying to do it the other way around.

I have an array of dates in the format 2013-02-25 but I want them to be js timestamps.

I have an array of dates such as ["2013-02-25", "2013-02-22", "2013-02-21"] and ive tried

new Date(dateArray[0]).getTime() / 1000;

but this gives the wrong result as "2013-02-25" is converted to 1361750400 which is Fri, 16 Jan 1970 18:15:50 GMT

any suggestions on how to do this in javascript please?


Solution

  • Update: Question was updated to use this code for trying to parse dates.

    Your problem now is that you do new Date(dateArray[0]).getTime() / 1000; . You shouldn't divide by 1000. Try new Date(dateArray[0]).getTime().


    I think the obvious solution is the one you missed :

    var date = new Date("2013-02-25")// contains Mon Feb 25 2013 02:00:00