Get the Current Year in JavaScript or Node.js

The JavaScript engine comes with a global Date class. Interacting with dates is an essential part of programming. JavaScript’s Date comes with two methods to retrieve the current year: Date#getYear and Date#getFullYear.

This tutorial shows you what to look out for when using one or another and walks you through the details!

Node.js Series Overview

Get the Current Year Number in JavaScript

Let’s say you may want to retrieve the year number in JavaScript to show a copyright hint in the footer of your website. Typically something like this:

© 2022 Future Studio. All Rights reserved

This use case requires only the year number. You don’t need the day or month of a date.

**Retrieve the year number from a JavaScript date using the getFullYear() method:

new Date().getFullYear()  
// 2022

JavaScript’s „Date#getYear“ is Deprecated

JavaScript also comes with a Date#.getYear() method. This method is deprecated because it returns the number of years since 1900.

That seems strange, but during the years until 1999 dates typically contained the year only as a two-digit number. For example, 1986 would be 86. But with the year 2000, the year number 86 isn’t unique anymore. Computers wouldn’t know if it means 1986 or 2086.

That’s why JavaScript introduced a method returning the full year. Please don’t use the Date#getYear method and prefer Date#getFullYear.

Here’s a sample output when using the deprecated method:

new Date().getYear()  
// 122 (which is the number of years since 1900)

Enjoy retrieving year numbers!

Explore the Library

Find interesting tutorials and solutions for your problems.