为什么 Date.parse(new Date()) 不等于 Date.now()

浏览器控制台
console.log(Date.parse(new Date()),Date.now(),new Date().getTime(),+new Date());
输出:
1555925489000 1555925489767 1555925489767 1555925489767
为什么Date.parse(new Date()) 舍弃了最后三位,不应该是毫秒级吗?
官网解释:
method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC

阅读 4k
2 个回答

据我猜测:调用Date.parse(new Date())时,JS引擎先会先调用new Date()的toString()方法,将其转换为字符串,而toString()转换的字符串只有精确到秒的表示,所以Date.parse之后,自然丢失了毫秒。

因为 Date.parse 接受的字符串类型,而 Date 对象的字符串形式类似 Mon Apr 22 2019 17:51:09 GMT+0800 (中国标准时间) 这样,其中是没有毫秒信息的

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