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

Q1037 Are there any Y2K date issues with JavaScript?

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

Given a date object:

var today = new Date();

when retrieveing the year value using:

var year = today.getYear();

the date may be returned as 99 for the year 1999 and 2000 for the year 2000.

For Y2K compliancy in Internet Explorer 4+ and Netscape Navigator 4+, you can use:

var year = today.getFullYear()

Which will always return a full four digit year.

To ensure that all browsers behave the same, do this:

var year = today.getYear();
if (year < 1000) year += 1900;

To simplify the process you could create a y2k() function that takes care of this for you:

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

var year  = y2k(today.getYear());

There is another possible Y2K issue with the document objects lastModified property, where using the general y2k() function to return a full ccyy date may not work. From what I can make out document.lastModified may return a two digit date - possibly even after 1999, when it might return 00 for 2000 and 01 for 2001. Using y2k() would make these dates 1900 and 1901 - not what you want to happen.

In which case you might want to try a combination of getFullYear() (where supported) and a check to see what side of an arbitary two digit year your date falls on:

<script language="JavaScript"><!--
function getCorrectedYear(year) {
    year = year - 0;
    if (year < 70) return (2000 + year);
    if (year < 1900) return (1900 + year);
    return year;
}

var lmDate = new Date(document.lastModified);

if (document.all || document.layers)
    year = lmDate.getFullYear();
else
    year = getCorrectedYear(lmDate.getYear());
//--></script>

Several servers send a non-standard value back as flastmod, which can result in a date of 1/1/1970 (plus or minus timezones.)

A page containing SSI may return a flastmod of 0 from most unix servers since any data on the page will not necessarily match the flastmod and so the page is considered created on the fly.

Weird results may occur on a Mac in Internet Explorer if the timezone is not set in the system.

Feedback on 'Q1037 Are there any Y2K date issues with JavaScript?'

©2018 Martin Webb