JavaScript

How to Get Current Year in JavaScript

The current day, date, and time along with the device’s time zone are returned by JavaScript’s Date object. Developers sometimes need to obtain the year in JavaScript in the standard order, whether the current year or the year following a given date. There are various ways to get the values of the Date object, including “getHours()”, “getFullYear()”, “getMonth()”, and many more to access the values of the Date object

This article will explain how to get a current year in JavaScript.

How to Get the Current Year in JavaScript?

To determine the current year in JavaScript, use the “Date.getFullYear()” method. According to local time, it returns the year of the specified day. The “getFullYear()” method returns an absolute number as its value.

Syntax
The below-given syntax is used to get the current year in JavaScript:

Date.getFullYear()

Return Value
It returns the four-digit integer value between 1000 and 9999 which indicates the year.

Example 1: Get Current Year in JavaScript
First, create a new Date object that returns the current date and time in the time zone that corresponds to the device:

var getYear = new Date();

Invoke the getFullYear() method with the date object, it gives the current year from the date:

console.log(getYear.getFullYear());

Output

The above output displays “2022” which is the current year.

But what If the user wants to get the year of the specified data in a specific format? In that case, follow the next example.

Example 2: Get Year of a Specified Date in JavaScript
Here, the constructor of the Date object is called by passing a custom date:

var getYear = new Date('Jan 14, 98 05:12:11');

Call the getFullYear() method with the date object, it gives the year from the specified date in a standard format:

console.log(getYear.getFullYear());

The corresponding output will look like this:

As the above output shows “1998” is the standard format of the year.

Conclusion

To determine the current year in JavaScript, use the “Date.getFullYear()” method. It will return an absolute number (four-digit integer) value. It gives the year of the current date as well as the specified date as an output. This article explains the procedure to get the current year in JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.