简体   繁体   English

javascript在不同的浏览器中的行为有所不同

[英]javascript behaves differently in different browsers

I am working on a project which need to deal with a lot of "dates". 我正在做一个需要处理很多“日期”的项目。 I notice that sometimes javascript behaves differently in different browsers: 我注意到有时javascript在不同的浏览器中的行为有所不同:

code: 码:

new Date("Mar 30, 2017".replace(',', '').replace(/ /g, '/'))

I know I don't need to use replace to create date, but this code gives me interesting different result from Chrome and Safari. 我知道我不需要使用replace来创建日期,但是这段代码为我提供了与Chrome和Safari不同的有趣结果。

While I run: 在我跑步时:

"Mar 30, 2017".replace(',', '').replace(/ /g, '/')

Both Chrome and Safari will gives me: "Mar/30/2017" . Chrome和Safari都将给我: "Mar/30/2017" But when I try to turn the result into a Date object, the interesting result will be: 但是,当我尝试将结果转换为Date对象时,有趣的结果将是:

Chrome: Thu Mar 30 2017 00:00:00 GMT-0700 (PDT)
Firefox: Date 2017-03-30T07:00:00.000Z
Safari: Invalid Date

I have already checked Javascript version of both browsers, and they are using the same version (1.7). 我已经检查了两个浏览器的Javascript版本,并且它们使用的是相同版本(1.7)。 Can anyone explain why Chrome behaves differently than Safari in this regard? 有谁能解释为什么Chrome在这方面的行为不同于Safari?

UPDATE I know "Mar/30/2017" is not a valid Date format. 更新我知道"Mar/30/2017"不是有效的日期格式。 But my question is why javascript behaves differently in different browsers . 但是我的问题是, 为什么javascript在不同的浏览器中表现不同 For those answers complaining about my Date format. 对于那些回答抱怨我的日期格式的答案。 Please read the question before answer it. 请先阅读问题,然后再回答。 I will take @Felix Kling 's answer, and thanks for all answers all the same. 我将使用@Felix Kling的答案,并感谢所有相同的答案。

Can anyone explain why Chrome behaves differently than Safari in this regard? 有谁能解释为什么Chrome在这方面的行为不同于Safari?

The specification says that handling unknown date formats is implementation dependent: 规范说,处理未知日期格式取决于实现:

[...] The String may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the String. [...]根据字符串的内容,字符串可以解释为本地时间,UTC时间或其他时区的时间。 The function first attempts to parse the format of the String according to the rules (including extended years) called out in Date Time String Format ( 20.3.1.16 ). 该函数首先尝试根据日期时间字符串格式( 20.3.1.16 )中调用的规则(包括扩展的年份)解析字符串的格式。 If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats. 如果字符串不符合该格式,则该函数可能会退回到任何特定于实现的启发式或特定于实现的日期格式。 [...] [...]

Safari and Chrome use different JavaScript engines and therefore their behavior can (and do) differ in this case. Safari和Chrome使用不同的JavaScript引擎,因此在这种情况下,它们的行为可能(也确实)有所不同。

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

相关问题 JavaScript删除对象在不同的​​浏览器中的行为有所不同 - JavaScript delete objects behaves differently in different browsers 事件代码在不同浏览器中的行为不同 - Event Code Behaves Differently in Different Browsers 相同的JavaScript代码在两种不同的环境中的行为不同 - Same JavaScript code behaves differently in two different environments 匿名函数名称在浏览器之间的行为有所不同吗? - Anonymous function name behaves differently between browsers? Window.Locaton.reload()在不同的浏览器(Firefox,Chrome)中的行为有所不同 - Window.Locaton.reload() behaves differently in different browsers (fire fox , Chrome) Javascript在Spotfire Webplayer中的行为有所不同 - Javascript behaves differently in Spotfire Webplayer javascript中的“ this”在Internet Explorer中的行为有所不同 - “this” in javascript behaves differently in Internet Explorer 为什么JavaScript“delete”运算符在不同的浏览器中表现不同? - Why does the JavaScript “delete” operator behave differently in different browsers? 在不同的浏览器中显示的颜色不同 - Colors shown differently in different browsers new Date()在不同环境中的行为不同,为什么? - new Date() behaves differently in different environment, why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM