new Date()vs Date(),为什么它返回不同的时间(-2小时)? [英] new Date() vs Date() and why does it return a different time (-2 hours)?
问题描述
我有这2个控制台日志,但是它们返回不同的时间(-2小时)。
console.log new Date())// Date 2015-04-20T15:37:23.000Z
console.log(Date())//Mon Apr 20 2015 17:37:23 GMT + 0200(CEST)$我知道使用Data()与使用构造函数和调用.toString()相同。
但是,我确实需要Date()时间,而不是新的Date()时间。那么为什么它返回错误的时间,我如何重置它输出正确的一个?
thx,
那么为什么它返回错误的时间,我如何重置它来输出正确的一个?
您的第一个语句是调用 console.log
与 Date
对象。看来,您使用的任何浏览器/控制台插件都可以选择通过在UTC中使用ISO-8601字符串来显示 Date
对象。例如,格式由控制台
实现选择。
您的第二个声明是调用 console.log
与字符串,因此格式由 Date
实现选择。 规范使不需要该字符串格式必须是从JavaScript引擎到JavaScript引擎(是的,真正的*),而不是它必须在本地时区。
显然,在您的浏览器,控制台
实现和 Date#toString
实现不匹配。
但是,我确实需要Date()时间,而不是新的Date()时间。
这些字符串定义了同一时刻(加或减几个微秒);只是字符串已经准备好了不同的时区设置。
如果您需要在本地时间将字符串登录到控制台,请使用
console.log(String(new Date()));
...可靠地从 Date
对象,而不是由控制台生成的东西
。
* Yup,您从 Date#toString
获取的格式未定义,完全取决于JavaScript实现;唯一的要求是 Date()
和 Date.parse()
都可以解析字符串 toString
输出并返回到原始日期。 (JavaScript甚至没有标准的日期/时间格式直到ES5 ,它只适用于 toISOString
方法,而不适用于 toString
,ES5略有错误它必须是在ES6中修改,导致当时Chrome做的一件事情不幸的情况,Firefox在新的日期/时间格式解析字符串时,没有时区。)
I have these 2 console logs, but they return different times (-2 hours off).
console.log(new Date()) // Date 2015-04-20T15:37:23.000Z
console.log(Date()) // "Mon Apr 20 2015 17:37:23 GMT+0200 (CEST)"
I know using Data() is the same as using the constructor and calling .toString().
However, i do need the Date() time and not the new Date() time. So why is it returning the wrong time and how can i reset it to output the correct one?
thx,
So why is it returning the wrong time and how can i reset it to output the correct one?
Your first statement is calling console.log
with a Date
object. It appears that whatever browser/console plugin you're using chooses to display that Date
object by using an ISO-8601 string in UTC. E.g., the format is chosen by the console
implementation.
Your second statement is calling console.log
with a string, so the format is chosen by the Date
implementation. The specification makes no requirement on what that string format must be from JavaScript engine to JavaScript engine (yes, really*) other than that it must be in the local timezone.
Apparently, on your browser, the console
implementation and the Date#toString
implementation don't match up.
However, i do need the Date() time and not the new Date() time.
Those strings define the same moment in time (plus or minus a couple of microseconds); it's just that the strings have been prepared with different timezone settings.
If you need to log a string to the console in local time, use
console.log(String(new Date()));
...to reliably get the string from the Date
object, not something generated by the console
.
* Yup, the format you get from Date#toString
is undefined and entirely up to the JavaScript implementation; the only requirement is that Date()
and Date.parse()
can both parse the string toString
outputs and get back to the original date. (JavaScript didn't even have a standard date/time format until ES5, and it only applies to the toISOString
method, not toString
. And ES5 got it slightly wrong and it had to be amended in ES6, leading to the unfortunate situation at the moment where Chrome does one thing and Firefox does another when parsing strings in the new date/time format with no timezone on them.)
这篇关于new Date()vs Date(),为什么它返回不同的时间(-2小时)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!