gpt4 book ai didi

javascript Date() 对象返回有效但不返回 new Date()

转载 作者:行者123 更新时间:2023-12-02 16:58:38 27 4
gpt4 key购买 nike

变量d = Date("12:00 AM")返回日期就好了,但是当尝试创建它的实例时 new Date("12:00 AM")它返回“无效日期”。

我找到了解决方案here ,使用 moment.js,效果很好,但我想知道为什么在使用“new Date()”时会出现“无效日期”

谢谢

fiddle

最佳答案

来自 MDN:

Note: Note that JavaScript Date objects can only be instantiated by calling JavaScript Date as a constructor: calling it as a regular function (i.e. without the new operator) will return a string rather than a Date object; unlike other JavaScript object types, JavaScript Date objects have no literal syntax.

...

Invoking JavaScript Date as a function (i.e., without the new operator) will return a string representing the current date and time.

调用的第一个版本会忽略参数(并且实际上不会生成日期)。

关于javascript Date() 对象返回有效但不返回 new Date(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25962156/

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