最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Weird error with Date, "uncaught illegal access" - Stack Overflow

programmeradmin5浏览0评论

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.

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.

Share Improve this question edited May 23, 2017 at 12:00 CommunityBot 11 silver badge asked Dec 22, 2014 at 7:51 rgajrawalargajrawala 2,1881 gold badge24 silver badges38 bronze badges 7
  • It's invalid all the way: jsfiddle/abhitalks/craz30o3 Chrome-39 on Win-8.1 here. – Abhitalks Commented Dec 22, 2014 at 8:00
  • @abhitalks Hmm, seems to error out when I use strings. See: jsfiddle/craz30o3/1 – rgajrawala Commented Dec 22, 2014 at 8:07
  • It works fine for other browsers. I tried in Firefox, Epiphany, also in Chromium. It succeeded and gave invalid date. The problem seems to be, only in Google-Chrome. – Venkata Krishna Commented Dec 22, 2014 at 8:23
  • @abhitalks Ah, makes sense. If you post this as an answer, I'll accept it. – rgajrawala Commented Dec 22, 2014 at 8:34
  • @usandfriends: Thanks. Added as answer and removing ments above. :) – Abhitalks Commented Dec 22, 2014 at 8:39
 |  Show 2 more ments

1 Answer 1

Reset to default 8
  1. As per specs here: http://www.ecma-international/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/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/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

发布评论

评论列表(0)

  1. 暂无评论