Date.parse not working

This question already has an answer here:

  • Why does Date.parse give incorrect results? 10 answers

  • JavaScript dates don't contain a time zone. The date you are parsing is interpreted as December 15, 2016 at midnight UTC.

    When you are converting it back to a string, it uses the time zone from your browser locale, and since you appear to be on the american east coast, the appropriate time zone is EST.

    7pm EST equals midnight the next day in UTC.


    Parse is not a reliable way to parse your date...

    Differences in assumed time zone

    Given a date string of "March 7, 2014", parse() assumes a local time zone, but given an ISO format such as "2014-03-07" it will assume a time zone of UTC (ES5 and ECMAScript 2015). Therefore Date objects produced using those strings may represent different moments in time depending on the version of ECMAScript supported unless the system is set with a local time zone of UTC. This means that two date strings that appear equivalent may result in two different values depending on the format of the string that is being converted.

    -- Source --

    I think you should get the client date with its timezone and use a library like Moment.js to parse the date correctly.


    You can create a date object from a specific UTC time using .Date.UTC() method, but then you have to pass the values individually:

    new Date(Date.UTC(year, month, day, hour, minute, second));

    A better and safer approach would be to use moment.js

    链接地址: http://www.djcxy.com/p/18628.html

    上一篇: javascript日期格式问题

    下一篇: Date.parse不起作用