Last Updated: July 31, 2017
·
1.401K
· wyuenho

How to get Jesus' birthday in Javascript

If you've never programmed a computer before 2000, you'll probably will be confused by this:

> new Date(1, 0, 1);
Tue Jan 01 1901 00:00:00 GMT+0800 (HKT)

Wha.. what? MDN's documentation says I can put the year as a number into the constructor and a JS Date can represent -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC. Let's see here, 1970 * 365 = 719050 days, I can't possibly have 99280950 leaps days can I?

Problem

Javascript's Date constructor has to handle pre-Y2K years so the number between 0 to 99 is treated as if they represent the years 1900 - 1999.

Solution

> var d = new Date(1, 0, 1) // Put the date in as usual
> d.setFullYear(1) // Now you get Jesus' birthday.

Ah hum, Jesus was born on what? BC 6 - 4?

3 Responses
Add your response

So, how old am I again?

over 1 year ago ·

@jjperezaguinaga Don't know, depends on whether the labor process took 1 year or 2 years.

over 1 year ago ·

@wyuenho Assuming Maria got pregnant by deity overnight, you never know! I also wonder what triggered you to figure out this information.

over 1 year ago ·