Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q301 Does setYear work correctly if it is given a two-digit year?

You are here: irt.org | FAQ | JavaScript | Date | Q301 [ previous next ]

Always use four digit dates, that way you are avoiding any potential millennium bugs in your code. Especially when accepting dates from user input.

setYear() will accept dates in two digit and four digit format, although it assumes that any two digit dates are for the 20th century, so that:

date = new Date(); // today
date.setYear(70);  // assumed to be 1970
date.setYear(99);  // assumed to be 1999

Whereas:

date = new Date();   // today
date.setYear(1970);  // is 1970
date.setYear(1999);  // is 1999
date.setYear(2000);  // is 2000
date.setYear(2001);  // is 2001
date.setYear(9999);  // is 9999

Note, in Netscape Navigator 2 you cannot use dates less than year dot. Year dot in JavaScript is Midnight January 1st 1970, i.e. the reference point. All dates are represented internally in JavaScript as the number of milliseconds since year dot. Netscape Navigator 2 does not allow negative millisconds, whereas Netscape Navigator 3, Netscape Navigator 4, Internet Explorer 4 and possibly Internet Explorer 3 do.

Therefore the following should not be done in Netscape Navigator 2, at best it'll crash the browser, at worse it'll crash the browser, and any other Netscape browser running, plus maybe the odd text editor running, and also any news and or mail programs that are Netscape products - BE WARNED.

date = new Date(1960);  // crash, burn and die

date = new Date();  // today
date.setYear(1950); // will not crash but the date will not be 1950

However the following all work in Netscape Navigator 3, Netscape Navigator 4 and Internet Explorer 4:

var date = new Date(-99999);  // 99999 milliseconds before year dot
var date = new Date(-999999999999); // a long time before 1970
var date = new Date(1950,1,1);
var date = new Date(1970,1,1);
var date = new Date();
var date = new Date(2000,1,1);
var date = new Date(9999,1,1);

var date = new Date();
date.setYear(1066);
date.setYear(1653);
date.setYear(1960);
date.setYear(1970);
date.setYear(1980);
date.setYear(1990);
date.setYear(9999);

©2018 Martin Webb