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:
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:
Invoke the getFullYear() method with the date object, it gives the current year from the date:
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:
Call the getFullYear() method with the date object, it gives the year from the specified date in a standard format:
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.