简体   繁体   English

Javascript日期表现不同

[英]Javascript date behaving differrently

I am executing the following in chrome developers console, 我在chrome开发人员控制台中执行以下命令,

var d = new Date("2010-03-20"); d
Fri Mar 19 2010 17:00:00 GMT-0700 (PDT)
var d = new Date("2010/03/20"); d
Sat Mar 20 2010 00:00:00 GMT-0700 (PDT)
var d = new Date("2010-3-20"); d
Sat Mar 20 2010 00:00:00 GMT-0700 (PDT)
var d = new Date("2010.3.20"); d
Sat Mar 20 2010 00:00:00 GMT-0700 (PDT)
var d = new Date("2010.03.20"); d
Sat Mar 20 2010 00:00:00 GMT-0700 (PDT)

My question is why is it behaving differently for "2010-03-20" where it is giving Mar 19 ? 我的问题是,为什么给予3月19日的“ 2010-03-20”行为会有所不同?

If I do getUTCDate(), I am getting 20, but why is it different only for 2010-03-20. 如果我执行getUTCDate(),我将得到20,但是为什么它仅在2010-03-20才有所不同。 Is it an active bug in javascript date ? 这是JavaScript日期中的活动错误吗?

It's not a bug, it's awkward but intended 这不是错误,很尴尬,但意在

The quick answer is that new Date("2010-03-20") is interpreted as a ISO-8601 date that, by default, is expressed in UTC (no timezone). 快速的答案是, new Date("2010-03-20")被解释为默认情况下以UTC(无时区)表示的ISO-8601日期。

Any other date string that don't fit the YYYY-mm-dd format ('.' instead of '-', '3' instead of '03') is read with the current client timezone information. 使用当前客户端时区信息读取不符合YYYY-mm-dd格式(“。”而不是“-”,“ 3”而不是“ 03”)的任何其他日期字符串。

And on top of that Date.prototype.toString() prints the date-time based on the client timezone. 在该Date.prototype.toString()顶部,将基于客户端时区显示日期时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM