Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tips for working with Pre-1000 A.D. Dates in JavaScript

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?

like image 890
broox Avatar asked May 02 '11 22:05

broox


People also ask

Can JavaScript handle date and time effectively?

The date and time is broken up and printed in a way that we can understand as humans. JavaScript, however, understands the date based on a timestamp derived from Unix time, which is a value consisting of the number of milliseconds that have passed since midnight on January 1st, 1970.

How do you check if a date is before another date in JavaScript?

To check if a date is before another date, compare the Date objects, e.g. date1 < date2 . If the comparison returns true , then the first date is before the second, otherwise the first date is equal to or comes after the second.


2 Answers

i prefer:

var d = new Date(Date.UTC(year, month, day, hour, min, sec, 0));
d.setUTCFullYear(year);

this always works for all supported year values.

the setUTCFullYear() call fixes JavaScript's intentional bug if you ask me.

like image 96
Shawn Kovac Avatar answered Sep 19 '22 16:09

Shawn Kovac


Have you tried using the setUTC... functions on a date object after its creation?

setUTCDate()
setUTCFullYear()
setUTCMonth()
setUTCHours()
setUTCMinutes()
setUTCSeconds()
like image 25
schnaader Avatar answered Sep 21 '22 16:09

schnaader