-3

On 1 jan 0099 there was Thrusday but it return. Friday

days = new Date(" January 1 ,0099")
day = days.getDay()
alert(day);

RESULT 5

But it should return 4

Ravi
  • 5
  • 1
  • 4
    _"year repeat after every 400 years"_ - What do you mean by that? – Turnip May 21 '20 at 11:49
  • 2
    January 1, 1999 was a Friday, so the result should be 5. – possum May 21 '20 at 11:51
  • Your assumption is wrong, same calendar doesn't repeat with 400 years period. – farincz May 21 '20 at 11:53
  • 1
    @Ivar That's not correct, `new Date(1900, 0, 1)` does just fine. – deceze May 21 '20 at 12:02
  • @deceze Interesting. Given that "_Date objects contain a Number that represents milliseconds since 1 January 1970 UTC_", I expected dates should be after that. But [apparently](https://stackoverflow.com/questions/11526504/minimum-and-maximum-date) that number can be negative and we can go back to the year -271821. – Ivar May 21 '20 at 12:09
  • @Ivar Since Date objects are not rarely used for birthdays, it would be very unreasonable for them to not at least support the past 100 years or so. – deceze May 21 '20 at 12:11

1 Answers1

4

Basically, it appears Javascript won't construct a Date in the year 99:

year
Integer value representing the year.

Values from 0 to 99 map to the years 1900 to 1999. All other values are the actual year.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#Syntax

You can try with different formats, 99 always appears to map to 1999. Likely this was implemented as a workaround and/or “convenience” for Y2K dates, perhaps even inherited from Java.

I'm not sure if there's a better workaround, but this works:

let d = new Date(100, 0, 1);
d.setFullYear(99);
deceze
  • 510,633
  • 85
  • 743
  • 889