SHARE
Facebook X Pinterest WhatsApp

JavaScript Y2k Fix

Written By
thumbnail
Joe Burns
Joe Burns
Jan 4, 2005

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]


 

Recommended for you...

The Revolutionary ES6 Rest and Spread Operators
Rob Gravelle
Aug 23, 2022
Ahead of Time (AOT) Compilation in Angular
Tariq Siddiqui
Aug 16, 2022
Converting a JavaScript Object to a String
Rob Gravelle
Aug 14, 2022
Understanding Primitive Type Coercion in JavaScript
Rob Gravelle
Jul 28, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.