JavaScript Date getUTCFullYear()
Syntax & Examples
Date.getUTCFullYear() method
The getUTCFullYear() method of the Date object in JavaScript returns the year (4 digits for 4-digit years) in the specified date according to universal time.
Syntax of Date.getUTCFullYear()
The syntax of Date.getUTCFullYear() method is:
getUTCFullYear()This getUTCFullYear() method of Date returns the year (4 digits for 4-digit years) in the specified date according to universal time.
Return Type
Date.getUTCFullYear() returns value of type Number.
✐ Examples
1 Using getUTCFullYear() to get the UTC year of a specific date
In JavaScript, we can use the getUTCFullYear() method to get the year from a Date object according to universal time.
For example,
- We create a new Date object
daterepresenting January 15, 2021. - We use the
getUTCFullYear()method to get the year fromdate. - The year is stored in the variable
utcYear. - We log
utcYearto the console usingconsole.log()method.
JavaScript Program
const date = new Date('2021-01-15');
const utcYear = date.getUTCFullYear();
console.log(utcYear);Output
2021
2 Using getUTCFullYear() with the current date
In JavaScript, we can use the getUTCFullYear() method to get the current year from the current date according to universal time.
For example,
- We create a new Date object
nowrepresenting the current date and time. - We use the
getUTCFullYear()method to get the current year fromnow. - The current year is stored in the variable
currentUTCYear. - We log
currentUTCYearto the console usingconsole.log()method.
JavaScript Program
const now = new Date();
const currentUTCYear = now.getUTCFullYear();
console.log(currentUTCYear);Output
2024
3 Using getUTCFullYear() with a specific date in the past
In JavaScript, we can use the getUTCFullYear() method to get the year from a specific date in the past according to universal time.
For example,
- We create a new Date object
pastDaterepresenting July 4, 1776. - We use the
getUTCFullYear()method to get the year frompastDate. - The year is stored in the variable
pastUTCYear. - We log
pastUTCYearto the console usingconsole.log()method.
JavaScript Program
const pastDate = new Date('1776-07-04');
const pastUTCYear = pastDate.getUTCFullYear();
console.log(pastUTCYear);Output
1776
Summary
In this JavaScript tutorial, we learned about getUTCFullYear() method of Date: the syntax and few working examples with output and detailed explanation for each example.