gpt4 book ai didi

javascript - 返回值 +new Date() 和 Date.now() 有区别吗?

转载 作者:行者123 更新时间:2023-11-29 10:35:27 25 4
gpt4 key购买 nike

假设我有两个函数

function newDate(){
return +new Date();
}
function dateNow(){
return Date.now();
}

所以我想知道一个返回 +new Date() 和另一个返回 Date.now() 是否有区别?

最佳答案

根据规范:

15.9.4.4 Date.now ( )
The now function return a Number value that is the time value designating the UTC date and time of the occurrence of the call to now.

15.9.3.3 new Date ( )
The [[PrimitiveValue]] internal property of the newly constructed object is set to the time value (UTC) identifying the current time.

使用一元 + 运算符将 new Date() 创建的 Date 对象强制转换为一个数字,得到原始值。所以你的两个案例是否总是相同的问题归结为以下两件事是否相同:

  1. 调用到现在的日期和时间
  2. 标识当前时间的时间值

看来他们是一个不错的赌注。

第三方编辑

chrome (v49) 控制台返回这个:

x = Date.now(), y1 = +new Date(), y2 = new Date();
x returns 1463201841680
y1 returns 1463201841680
y2 returns Sat May 14 2016 06:57:21 GMT+0200

关于javascript - 返回值 +new Date() 和 Date.now() 有区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37196227/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com