奇怪的错误与日期,“未捕获的非法访问” [英] Weird error with Date, "uncaught illegal access"

查看:94
本文介绍了奇怪的错误与日期,“未捕获的非法访问”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图找到Javascript可以处理的最新 Date



我把它归结为9月275760,并增加了我开始从 new Date('09 / 24/275760')获取未被捕获的非法访问异常的日子c $ c>到新日期('10 / 13/275760')。在之前新日期('09 / 24/275760')新日期正常工作并给我无效日期以及新日期('10 / 13/275760')新日期再次开始正常工作,给了我无效日期



我查看了错误堆栈,起源于命令行界面,这并没有真正的帮助(除了它表示错误必须来自内部代码)。



所以问题为什么新日期在这些特定日期抛出一个错误,而不是遵循给定无效日期的定义行为?



我在Mac OSX Yosemite(10.10.3)上运行Chrome 43.0.2357.81(64位)(官方版本)。



编辑



这似乎只在使用字符串时才会发生(EX: new Date('10 / 01/275760 ))。看到这个 JSFiddle 。但是,当使用整数参数(EX: new Date(275760,10,1))时,它似乎工作正常。感谢 @abhitalks 发现此问题。

解决方案


  1. 根据这里的规范: http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.1 :1970年1月1日的实际时间范围为8640,000,000,000,000毫秒世界标准时间。因此,您将获得的最大有效日期是9月13日275760而不是9月23日。

  2. 对于日期(字符串),字符串值应该在ISO8601中(规格如下: http://www.w3.org/TR/NOTE-datetime )这是yyyy-mm-dd。任何其他格式都被解析为本地时间,并且会受到回退到实现特定的启发式的影响。所以,你看到的行为是特定于实现的。 (根据: https://developer.mozilla .org / en-US / docs / Web / JavaScript / Reference / Global_Objects / Date / parse

实际目的,最大日期应该表示为:

  var dt = new Date(8640000000000000)

,13); var dt2 = new Date(275760,9,14); var dt3 = new Date(8640000000000000); var dt4 = new Date('275760-9-13'); console.log(dt0); // Sat Sep 13 275760 00:00:00 GMTconsole.log(dt1); //无效的日期(读取十月份,基于0的月份)console.log(dt2); //无效的Dateconsole.log(dt3); // Sat Sep 13 275760 05:30:00 GMTconsole.log(dt4); // Sat Sep 13 275760 00:00:00 GMT


So I was trying to find the latest Date that Javascript could handle.

I got it down to September 275760 and incremented the days when I started to get an uncaught illegal access exception from new Date('09/24/275760') to new Date('10/13/275760'). Before new Date('09/24/275760'), new Date was working normally and giving me Invalid Date and after new Date('10/13/275760'), new Date started working normally again and gave me Invalid Date.

I looked at the error stack and it originated from the Command Line Interface, which wasn't really helpful (except for the fact that it signified that the error must have originated from internal code).

So the question is, why is new Date throwing an error instead of following the defined behavior of giving Invalid Date on these specific dates?

I'm running Chrome 43.0.2357.81 (64-bit) (Official Build) on Mac OSX Yosemite (10.10.3).

Edit

This seems to happen only when I use strings (EX: new Date('10/01/275760')). See this JSFiddle. However, when using the integer arguments (EX: new Date(275760, 10, 1)), it seems to work fine. Thanks to @abhitalks for finding this.

解决方案

  1. As per specs here: http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.1 : The actual range of times is 8,640,000,000,000,000 milliseconds to either side of 01 January, 1970 UTC. So, the maximum valid date you will get is "Sep 13 275760" not "Sep 23".
  2. For the Date(string), the string value should be in ISO8601 (Spec here: http://www.w3.org/TR/NOTE-datetime) which is "yyyy-mm-dd". Any other format is parsed as local time and is subject to "fall-back to implementation-specific heuristic". So, the behaviour you are seeing is implementation specific. (As per: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse)

For all practical purposes, the max date should be represented as:

var dt = new Date(8640000000000000)

var dt0 = new Date(275760, 8, 13);
var dt1 = new Date(275760, 9, 13);
var dt2 = new Date(275760, 9, 14);
var dt3 = new Date(8640000000000000);
var dt4 = new Date('275760-9-13');

console.log(dt0); // Sat Sep 13 275760 00:00:00 GMT
console.log(dt1); // Invalid Date (Reads October, 0-based month)
console.log(dt2); // Invalid Date
console.log(dt3); // Sat Sep 13 275760 05:30:00 GMT
console.log(dt4); // Sat Sep 13 275760 00:00:00 GMT

这篇关于奇怪的错误与日期,“未捕获的非法访问”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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