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:
Returns:
- An integer representing the year (e.g., 2024).
Example 1: Getting UTC Year for a Specific Date
Output:
Explanation:
- The
Date
object represents October 22, 2024, at 15:30:00 UTC. ThegetUTCFullYear()
method returns2024
, which is the year in UTC.
Example 2: Getting UTC Year for a Local Time
Output:
Explanation:
- Even if the date is created in local time,
getUTCFullYear()
still returns2024
for October 22, 2024, since it corresponds to the same year in UTC.
Example 3: Getting UTC Year for a Date Near Midnight
Output:
Explanation:
- Regardless of the local time, as long as the date remains within the same year,
getUTCFullYear()
will return2024
.
Example 4: Getting UTC Year for a Date Object Created with UTC
Output:
Explanation:
- The
Date.UTC()
method creates a date object for October 22, 2024, in UTC. ThegetUTCFullYear()
method will return2024
, confirming that the year is indeed 2024 in UTC.
Summary:
date.getUTCFullYear()
returns the year of aDate
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.