Fix It!
Let's think this through. We need to create a blip of code that will work across all browsers. We already know that getFullYear() does not work across all browsers. The outdated getYear() does.
I know we can go nuts setting different blips of code depending on browser and version number, but let me suggest we go with what we know works and try to simplify it a bit. To figure a year, try this:
<SCRIPT LANGUAGE="JavaScript"> RightNow = new Date();
var TheYear = RightNow.getYear()
if (TheYear >= 100 && TheYear <= 1999)
{TheYear=TheYear + 1900}
else
{TheYear=TheYear}
document.write("The year is " + TheYear + ". ")
</Script>
Here's the script in action. Is the year correct?
How it Works
Again, we know getYear() works on more browsers than getFullyear(). So, let's use that to set the year. We assign that value to a variable "ThisYear".
We grab the year and test it:
if (TheYear >= 100 && TheYear <= 1999)
We know that a browser will return one of two values during the year 2000: "100" or "2000". The line above tests if the value returned is greater than or equal to 100. If it is, we then test to see if it is less than or equal to 1999. Remember, we do not want any returns that are already 2000. By testing this wide range of numbers, this format will continue working for hundreds of years.
{TheYear=TheYear + 1900}
else
{TheYear=TheYear}
If "TheYear" value is between 100 and 1999, we add 1900. Again, this will work well into the future as adding 1900 will give us 2001 next year when the "getYear()" count is up to 101.
If the return for "getYear()" happens to be 2000, then leave it alone.
Now, anytime you require the year in the script, call for the variable "ThisYear". I did it in this document.write statement:
document.write("The year is " + TheYear + ". ")
That's That
I think this is the simplest and most efficient method for getting the correct date across the most number of browsers. Use it in good health.
Enjoy!
[The Problem]
[The Version Problem]
[Fix It!]
[How It Works]