JavaScript 中的日期与新日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9584719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Date vs new Date in JavaScript
提问by Jonathan
new Date()
takes an ordinal and returns a Date
object.
What does Date()
do, and how come it gives a different time?
new Date()
接受一个序数并返回一个Date
对象。
有什么作用Date()
,为什么它会给出不同的时间?
>>> new Date(1329429600000)
Date {Fri Feb 17 2012 00:00:00 GMT+0200 (????? Standard Time)}
>>> Date(1329429600000)
"Tue Mar 06 2012 15:29:58 GMT+0200 (Jerusalem Standard Time)"
回答by pimvdb
From the specs:
从规格:
When
Date
is called as a function rather than as a constructor, it returns a String representing the current time (UTC).
当
Date
作为函数而不是构造函数调用时,它返回一个表示当前时间 (UTC) 的字符串。
and:
和:
When
Date
is called as part of anew
expression, it is a constructor: it initialises the newly created object.
当
Date
作为new
表达式的一部分被调用时,它是一个构造函数:它初始化新创建的对象。
So, new Date(...)
returns an object such that obj instanceof Date
is true, whereas Date(...)
basically returns the same as new Date().toString()
.
因此,new Date(...)
返回一个obj instanceof Date
为真的对象,而Date(...)
基本上返回与new Date().toString()
.
回答by Jonathan
new Date
creates a new Date object that you can modify or initialize with a different date while Date
returns a string of the current date/time, ignoring its arguments.
new Date
创建一个新的 Date 对象,您可以使用不同的日期修改或初始化该对象,同时Date
返回当前日期/时间的字符串,忽略其参数。
回答by nkron
Check out JavaScript Datefor a quick API reference and code test bed. You can see the Date()
function called without new
does not take any parameters and always returns a string
representation of the current date/time. If you modify the sample to be:
查看JavaScript Date以获得快速的 API 参考和代码测试平台。您可以看到Date()
调用 without的函数new
不带任何参数,并且始终返回string
当前日期/时间的表示。如果您将示例修改为:
console.log(Date());
console.log(Date(1329429600000));
You'll find the results for both are the same (because JavaScript ignores extra arguments passed to functions):
你会发现两者的结果是一样的(因为 JavaScript 会忽略传递给函数的额外参数):
Wed Apr 11 2012 09:58:11 GMT-0700 (PDT)
Wed Apr 11 2012 09:58:11 GMT-0700 (PDT)
回答by pollx
It's 2017 and I had the same question in mind. What I found as an answer after some reading:
现在是 2017 年,我也有同样的问题。我在阅读后发现的答案是:
"The simplest way to perform an explicit type conversion is to use the Boolean() , Number() , String() , or Object() functions. We've already seen these functions as constructors for wrapper objects. When invoked without the new operator, however, they work as conversion functions and perform type conversions.."
“执行显式类型转换的最简单方法是使用 Boolean() 、 Number() 、 String() 或 Object() 函数。我们已经将这些函数视为包装对象的构造函数。在没有 new 的情况下调用时运算符,但是,它们用作转换函数并执行类型转换..”
"The built-in classes of core JavaScript attempt valueOf() conversion before toString() conversion, except for the Date class, which performs toString() conversion."
“核心 JavaScript 的内置类在 toString() 转换之前尝试 valueOf() 转换,但 Date 类除外,它执行 toString() 转换。”
So Date() invoked without the new keyword performs a type conversion. And since Date is an object and an object-to-primitive should happen, date objects by default call toString() (though Date has meaningful valueOf() method as well).
因此,在没有 new 关键字的情况下调用 Date() 执行类型转换。并且由于 Date 是一个对象并且应该发生对象到原始对象,因此日期对象默认调用 toString() (尽管 Date 也具有有意义的 valueOf() 方法)。
Found that on the "JavaScript: The Definitive Guide" book. Leaving it here for the future generations who have just started learning JS :)
在“JavaScript: The Definitive Guide”一书中找到的。留给刚开始学习 JS 的后代吧:)
回答by Abdennour TOUMI
Date
class can be called as constructoror as methodto have a built-in code like :
Date
类可以作为构造函数或方法调用以具有内置代码,例如:
function Date(args){
if (this.constructor == Date){
// if you call : new Date(args)
}else{
// if you call as method : Date()
return new Date()
}
}
Thus , if you called it like method , it re-call the constructor to return the current date&time .
因此,如果您像方法一样调用它,它会重新调用构造函数以返回当前日期和时间。
回答by PraveenVenu
new Date()
returns the date based on the input parameter and Date()
returns todays date on the browser.
new Date()
根据输入参数Date()
返回日期并在浏览器上返回今天的日期。
回答by mav
Date lets you create objects that represent date/time. It's NOT meant to be called like a function. You can get more information here: Date - MDN
Date 允许您创建表示日期/时间的对象。它并不意味着像函数一样被调用。您可以在此处获取更多信息:日期 - MDN
回答by S.Munkhey
Calling a constructor as a Function is plain wrong it'll do (probably) unexpected things with your app scope and before very long you'll be the focus of attention in a group bug fixing session.
将构造函数作为函数调用是完全错误的,它会对你的应用程序范围做(可能)意想不到的事情,不久你就会成为小组错误修复会议的关注焦点。
Create a Date Object as intended by the designers of the spec, don't code to the workarounds implemented as safeguards by engineers that think JS programmers are stupid. (worked in the lab, was in the next chair during the conversation, dealt with it and moved on)
按照规范设计者的意图创建一个日期对象,不要对那些认为 JS 程序员是愚蠢的工程师作为保护措施实施的变通方法进行编码。(在实验室工作,在谈话期间在下一张椅子上,处理它并继续前进)
If you are madly against new you can try object.create but at time of writing it's slower and unless you are planning to implement polymorphic inheritance then it is extra effort for less reward.
如果你疯狂地反对 new 你可以尝试 object.create 但在编写它时它会更慢,除非你计划实现多态继承,否则它是额外的努力以获得更少的回报。