gpt4 book ai didi

javascript - JavaScript 中 Date(d.getTime()) 和 new Date(d.getTime()) 有什么区别?

转载 作者:行者123 更新时间:2023-12-01 00:22:12 31 4
gpt4 key购买 nike

我一直在处理日期并实现了这两个命令 Date(d.getTime())new Date(d.getTime())是不同的。

当我运行此代码片段时:

        var d = new Date(2016,11,12);
console.log(Date(d.getTime()));
console.log(new Date(d.getTime()));

我有这个结果:

(index):68 Thu Dec 12 2019 18:02:41 GMT-0300 (Horário Padrão de Brasília)
(index):69 Mon Dec 12 2016 00:00:00 GMT-0200 (Horário de Verão de Brasília)

为什么它们不同?

我试图寻找一些答案,但没有找到。这些是我浏览过的一些引用资料:

Difference between Date(dateString) and new Date(dateString)

Why we can't call methods of Date() class without new operator

http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.2

最佳答案

Date 获取 window/global Date 对象,new Date() 创建一个新的静态 日期对象。当您创建一个new Date()时,您会及时卡住它的值。

当您使用Date()时,它会返回当前日期的字符串表示形式,就像调用new Date().toString()一样。

根据您的代码:

// this is a new date, Dec. 12, 2016
var d = new Date(2016,11,12);

// this returns the time value of Date d
d.getTime()

// Calling Date as a function just returns a string for
// the current date, arguments are ignored
console.log(Date(d.getTime()));

// this creates a new date based on d's time value
console.log(new Date(d.getTime()));

关于javascript - JavaScript 中 Date(d.getTime()) 和 new Date(d.getTime()) 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59312685/

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