Javascript New Date() returns invalid date

This question already has an answer here:

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

  • Assuming the browser is in a time zone with a UTC offset of +2 at the start of July 24th 2017, it's behaving as documented.

    The Date constructor is documented as behaving like Date.parse , which then includes this documentation - along with multiple warnings not to use the constructor accepting a string, or the parse method :

    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.

    So it's providing you July 24th 2017 at midnight in the local time zone.

    It sounds to me like you'd be better off using Moment.js or something similar to give you clearer control over the parsing/formatting.


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

    试试这种格式(UTC将是您日期的时区)

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

    上一篇: JavaScript新日期与字符串参数有错误的日期

    下一篇: Javascript新日期()返回无效日期