JavaScript Date getFullYear() Method

The getFullYear() method in JavaScript is used to get the four-digit year for the specified date according to local time.

Here is an example of how to use the getFullYear() method:

const date = new Date();
const year = date.getFullYear();
console.log(year); // output: 2022 (assuming it is currently 2022)

You can also pass a date as an argument to the Date constructor to get the year for a specific date:

const date = new Date('January 1, 2021');
const year = date.getFullYear();
console.log(year); // output: 2021

Note that the getFullYear() method returns the year in the local timezone. If you want to get the year in a specific timezone, you can use the getUTCFullYear() method instead.

For example:

const date = new Date();
const utcYear = date.getUTCFullYear();
console.log(utcYear); // output: 2022

I hope this helps! Let me know if you have any questions.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: