JavaScript中的日期与新日期 [英] Date vs new Date in JavaScript
问题描述
new Date()
接受序数并返回 Date
对象。
什么日期()
这样做,以及如何给出不同的时间?
new Date()
takes an ordinal and returns a Date
object.
What does Date()
do, and how come it gives a different time?
>>> 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)"
推荐答案
来自规范:
当
Date
作为函数而不是构造函数调用时,它返回表示当前的String时间(UTC)。
When
Date
is called as a function rather than as a constructor, it returns a String representing the current time (UTC).
和:
当
Date
作为new
表达式的一部分被调用时,它是一个构造函数:它初始化新创建的对象。
When
Date
is called as part of anew
expression, it is a constructor: it initialises the newly created object.
所以,新日期(...)
返回一个对象 obj instanceof Date
为true,而 Date(...)
基本上返回与 new Date()。toString()
。
So, new Date(...)
returns an object such that obj instanceof Date
is true, whereas Date(...)
basically returns the same as new Date().toString()
.
这篇关于JavaScript中的日期与新日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!