• JavaScript Video Tutorials

JavaScript Date getFullYear() Method



The Date.getFullYear() method in JavaScript is used to retrieve the "full year" (according to the local time) for a specified date. It does not accept any parameters insteead; it returns the year from a Date object as a four-digit integer (between between year 1000 and 9999).

It is recommended to use this method to retrieve the "full year" from a date instead of the getYear() method.

Syntax

Following is the syntax of JavaScript Date getFullYear() method −

getFullYear();

This method does not accept any parameters.

Return Value

This method returns a four-digit integer representing the year component of the date.

Example 1

In the following example, we are demonstrating the basic usage of JavaScript Date getFullYear() method −

<html>
<body>
<script>
   const currentDate = new Date();
   const year = currentDate.getFullYear();

   document.write(year);
</script>
</body>
</html>

Output

The above program returns the full year of the specified date.

Example 2

Here, we are retrieving full year for the specific date "December 20, 2020" −

<html>
<body>
<script>
   const currentDate = new Date('2020-12-20');
   const year = currentDate.getFullYear();

   document.write(year);
</script>
</body>
</html>

Output

The above programs returns integer "2020" as the full year for given date.

Example 3

In the following example, we are adding 5 years to the Current year −

<html>
<body>
<script>
   const currentYear = new Date().getFullYear();
   const result = currentYear + 5;

   document.write(`Current Year: ${currentYear}` + "<br>");
   document.write(`After adding (5) years: ${result}`);
</script>
</body>
</html>

Output

As we can see the output, 5 years has been added to the current year.

Advertisements