I'm curious if anyone has any good solutions for accurately building dates prior to the year 1000 A.D. - particularly the years 1 - 100 AD.
For example, if I want to build a date for the start of the 1st millenium, I can't just do...
new Date(Date.UTC(1,0,1,0,0,0,0));
because it tries to be "smart" and assume that 1 is 1901, which gives me...
Sun Dec 31 1900 18:00:00 GMT-0600 (CST)
The same thing goes for the year 99...
new Date(Date.UTC(99,0,1,0,0,0,0));
which becomes
Thu Dec 31 1998 18:00:00 GMT-0600 (CST)
Thoughts?
Others have provided hints at a fix. The javascript date object was copied from Java, so has all its bugs too. There is not much point to Gregorian dates before 1582 anyway since before that various other calendars were in use.
Here is the basic solution I came up with. If a date is prior to year 1000, I just add a 1000 to it while constructing the date, then use
setUTCFullYear()
afterwards.1000 may be overkill since I was only having problems with pre-100 dates... but, whatever.
Have you tried using the setUTC... functions on a date object after its creation?
i prefer:
this always works for all supported year values.
the setUTCFullYear() call fixes JavaScript's intentional bug if you ask me.
It's exactly what
Date.UTC
and theDate
constructor function (called with numbers as arguments) are supposed to do. A simple workaround is to useDate.parse
, which will not apply any correctionsMake some arbitrary date
d
and calld.setUTCFullYear(myDate)
. This seems to be working for me in Chrome's console.