javascript new Date with string param has wrong date

This question already has an answer here:

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

  • Date.toString() is formatted in your local time zone, but because you've passed in an ISO-8601 string, the value is parsed as if it's UTC.

    From the Date.parse() documentation (as the Date(String) constructor is documented to behave like Date.parse ):

    The date time string may be in a simplified ISO 8601 format. For example, "2011-10-10" (just date) or "2011-10-10T14:48:00" (date and time) can be passed and parsed. Where the string is ISO 8601 date only, the UTC time zone is used to interpret arguments. If the string is date and time in ISO 8601 format, it will be treated as local.

    So you'll end up with a Date which is equivalent to 2017-12-06T00:00:00Z. But Date.toString() shows you that instant in time in your current time zone - and if you're in America/New_York or a similar time zone which is 5 hours behind UTC at that moment in time, that means it'll print December 5th at 7pm.

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

    上一篇: 将JavaScript字符串转换为日期

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