JavaScript date.getUTCFullYear() method


The date.getUTCFullYear() method in JavaScript returns the year of a specified Date object according to Universal Coordinated Time (UTC). This method is useful for obtaining the year without regard to local time zone differences.

Syntax:

date.getUTCFullYear();

Returns:

  • An integer representing the year (e.g., 2024).

Example 1: Getting UTC Year for a Specific Date

const date = new Date('2024-10-22T15:30:00Z'); // October 22, 2024, at 15:30:00 UTC const utcYear = date.getUTCFullYear(); console.log(utcYear);

Output:

2024

Explanation:

  • The Date object represents October 22, 2024, at 15:30:00 UTC. The getUTCFullYear() method returns 2024, which is the year in UTC.

Example 2: Getting UTC Year for a Local Time

const localDate = new Date('2024-10-22T12:00:00'); // October 22, 2024, at 12:00 PM local time const utcYearFromLocal = localDate.getUTCFullYear(); console.log(utcYearFromLocal);

Output:

2024

Explanation:

  • Even if the date is created in local time, getUTCFullYear() still returns 2024 for October 22, 2024, since it corresponds to the same year in UTC.

Example 3: Getting UTC Year for a Date Near Midnight

const nearMidnightLocal = new Date('2024-10-22T23:59:59'); // October 22, 2024, at 23:59:59 local time const utcYearNearMidnight = nearMidnightLocal.getUTCFullYear(); console.log(utcYearNearMidnight);

Output:

2024

Explanation:

  • Regardless of the local time, as long as the date remains within the same year, getUTCFullYear() will return 2024.

Example 4: Getting UTC Year for a Date Object Created with UTC

const utcDate = new Date(Date.UTC(2024, 9, 22)); // October is month 9 (0-indexed) const utcYearCreated = utcDate.getUTCFullYear(); console.log(utcYearCreated);

Output:

2024

Explanation:

  • The Date.UTC() method creates a date object for October 22, 2024, in UTC. The getUTCFullYear() method will return 2024, confirming that the year is indeed 2024 in UTC.

Summary:

  • date.getUTCFullYear() returns the year of a Date object in UTC.
  • It provides a reliable way to retrieve the year without being affected by local timezone offsets, making it useful for applications that require consistent date handling across different time zones.