JavaScript中的日期与新日期 [英] Date vs new Date in JavaScript

查看:85
本文介绍了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 a new 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆