JavaScript Showing Wrong Year For Date

Share this article

Quick post to let you JavaScript newbies out there that when declaring a date it might be showing the wrong year for the date. You may be thinking “javascript date returning later year, I was seeing 2012 instead of 2011, wtf?”. Consider the following examples: So the date we want to see is Friday 15 July 2011.

var todaysDate = new Date();
console.log(todaysDate);
//output: Date {Fri Aug 12 2011 18:45:53 GMT+1000}

var expiryDate = new Date('15/07/2011');
console.log(expiryDate);
//output: Date {Wed Mar 07 2012 00:00:00 GMT+1000}

var expiryDate = new Date('07/15/2011');
console.log(expiryDate);
//output: Date {Fri Jul 15 2011 00:00:00 GMT+1000}
Can you work out whats going on? Well, JavaScript’s getMonth() function starts with 0 for January, 1 for February and so on… (you have to add one!) but this is the wrong year, day everything! If we try parsing the date as single arguments this also doesn’t produce the correct results.
var expiryDate = new Date(2011, 15, 07);
console.log(expiryDate);
//output: Date {Sat Apr 07 2012 00:00:00 GMT+1000}
But, if we parse as a text date we get the correct results! :)
var expiryDate = new Date('July 15, 2011');
console.log(expiryDate);
//output: Date {Fri Jul 15 2011 00:00:00 GMT+1000}
Date’s can be a pain the A$$ so if your doing a lot of work with dates I would suggest to use a JavaScript DATE library to make managing dates in JavaScript a wizz! This is an awesome date resource
too (very comprehensive!).

Frequently Asked Questions (FAQs) about JavaScript Date Functions

Why does JavaScript show the wrong year when using the getYear() method?

The getYear() method in JavaScript returns the year in the date as a two-digit number, subtracting 1900 from the actual year. For example, if the year is 2022, getYear() will return 122. This can lead to confusion and incorrect data representation. To avoid this, it’s recommended to use the getFullYear() method, which returns the full four-digit year.

What is the difference between getYear() and getFullYear() in JavaScript?

The primary difference between getYear() and getFullYear() lies in the format of the returned year. getYear() returns the year minus 1900, resulting in a two-digit number for years after 2000. On the other hand, getFullYear() returns the full four-digit year, making it more accurate and less confusing.

How can I correct the year returned by the getYear() method?

To correct the year returned by the getYear() method, you can add 1900 to the result. However, it’s generally recommended to use the getFullYear() method instead, as it directly returns the correct four-digit year.

Why does JavaScript use 1900 as the base year in the getYear() method?

The getYear() method was designed this way for historical reasons related to the Y2K problem. However, this design can lead to confusion and errors, so it’s generally recommended to use the getFullYear() method instead.

Can I use the getYear() method to get the current year?

While you can technically use the getYear() method to get the current year by adding 1900 to the result, it’s generally recommended to use the getFullYear() method instead, as it directly returns the correct four-digit year.

How can I get the current year in JavaScript?

You can get the current year in JavaScript by creating a new Date object and calling the getFullYear() method on it. For example: let currentYear = new Date().getFullYear();

What is the return type of the getYear() and getFullYear() methods?

Both the getYear() and getFullYear() methods return a number representing the year. getYear() returns the year minus 1900, while getFullYear() returns the full four-digit year.

Are the getYear() and getFullYear() methods zero-based like JavaScript arrays?

No, the getYear() and getFullYear() methods are not zero-based. They return the actual year, although getYear() subtracts 1900 from it.

Can I use the getYear() and getFullYear() methods on dates in the future or past?

Yes, you can use the getYear() and getFullYear() methods on any valid Date object, regardless of whether the date is in the future or past.

Are there any browser compatibility issues with the getYear() and getFullYear() methods?

Both the getYear() and getFullYear() methods are well-supported in all modern browsers. However, because of the potential for confusion and errors with getYear(), it’s generally recommended to use getFullYear() instead.

Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

jQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form