关于new Date() 参数格式的问题

  1. 最近在使用new Date()取时间戳的时候,发现一个问题。相同的参数不同的表现形式,返回值不一样
  2. 如图所示
    图片描述
  3. 发现该类型的时间格式,返回值会多8小时,其他类型就不会。
  4. 请问为什么会有这种现象呢?
阅读 3.4k
1 个回答

Date 解析字符串时,只有这一种格式是有一致定义的:YYYY-MM-DDTHH:mm:ss.sssZ

这这种格式里,如果没有时间部分,那么就是 UTC 的 00:00:00 ,也就是中国的 08:00:00 (如果有时间部分,那么当作本地时间解释)

All numbers must be base 10. If the MM or DD fields are absent "01" is used as the value. If the HH, mm, or ss fields are absent "00" is used as the value and the value of an absent sss field is "000". When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time.

其他格式,都是实现定义的:

If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.

分别提供年月日 ... 等等的构造Date ( year, month [ , date [ , hours [ , minutes [ , seconds [ , ms ] ] ] ] ] ) 用的是本地时间(你的倒数第二个调用)。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题