Thursday, December 12, 2024

JavaScript Y2k Fix

Use these to jump around or read it all…


[The Problem]

[The Version Problem]

[Fix It!]

[How It Works]

     On January 1st, 2000, the Y2K bug didn’t bite very hard. In fact, it was a bust.
Where the bug is nibbling a bit is inside of a lot of people’s JavaScripts. It’s not that code hasn’t been created to solve the problem, it’s that the code isn’t universal. Here’s the scoop:



The Year Problem

     Most of you are familiar with this format:

<SCRIPT LANGUAGE=”JavaScript”>

RightNow = new Date();
document.write(“The year is ” + RightNow.getYear() + “. “)
</SCRIPT>

     The concern is that getYear() method. It only returns a two-digit year. Until the year 2000, that worked just fine. Now (at least until 2001) that method is returning “100”. That’s why so many sites had their date written January 1, 19100.
The “19” was put in as text and the getYear() returned a 100.

The Fix!!

     Hooray! A new method was brought to the fold to correct the problem, getFullYear().
That method returned a full four-digit year. It works great…as long as your browser understands it.



The Version Problem

     So it’s fixed right? Just go into your scripts, and change out getYear() with getFullYear() and all will be well, right?

     Wrong.

     The command getFullYear() only works with Netscape Navigator version 4.0 and above. The command only works with some Internet Explorer versions 3.x and Internet Explorer 4 and above.

     Monkey wrench! The method getYear() works with Internet Explorer 5 returning the full four-digit year.

     Isn’t this fun?



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]


 

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured