Date object and is used to get the full year (4-digit year) for a given date object, in the UTC time zone. This can be useful for getting the year in a specific time zone, or for working with dates in a consistent time zone.
Here’s an example of how to use
getUTCFullYear() to get the current year in UTC time:
let date = new Date(); let year = date.getUTCFullYear(); console.log(year); // Outputs the current year in UTC time
You can also pass a date object as an argument to
getUTCFullYear() to get the year for a specific date in UTC time:
let date = new Date(2022, 0, 1, 11, 30, 0); // January 1, 2022 at 11:30:00 (local time) let year = date.getUTCFullYear(); console.log(year); // Outputs 2022
Note that the
getUTCFullYear() method returns the year as a 4-digit number. If you want to get the year as a 2-digit number, you can use the
getUTCYear() method instead. However, this method is not widely supported and may not work in all browsers.
In addition to getting the year, you can also use the
setUTCFullYear() method to set the year for a date object in UTC time. Here’s an example of how to use it:
let date = new Date(); date.setUTCFullYear(2023); // Set the year to 2023 console.log(date); // Outputs the current date and time with the year set to 2023 in UTC time
Leave a Reply