JavaScript data.getFullYear() method


The date.getFullYear() method in JavaScript returns the full year (as a four-digit number) for a specific Date object, according to local time.

Syntax:

date.getFullYear();

Returns:

  • A number representing the year (e.g., 2024).

Example 1: Getting the Full Year for a Specific Date

const date = new Date('2024-10-22'); // October 22, 2024 const year = date.getFullYear(); console.log(year);

Output:

2024

Explanation:

  • The getFullYear() method retrieves the full year (2024) from the Date object for October 22, 2024.

Example 2: Getting the Current Year

const today = new Date(); // Current date const currentYear = today.getFullYear(); console.log(currentYear);

Output:

(Depends on the current year, e.g., 2024)

Explanation:

  • This retrieves the current year from the system's date and time.

Example 3: Getting the Year for an Earlier Date

const date = new Date('1995-12-17'); // December 17, 1995 const year = date.getFullYear(); console.log(year);

Output:

1995

Explanation:

  • For the date "1995-12-17", the getFullYear() method returns 1995.

Summary:

  • date.getFullYear() returns the full four-digit year of the given date.
  • It is commonly used to retrieve the year for any Date object in JavaScript, including both past and future dates.