DEV Community

Cover image for How to get the current year using JavaScript?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to get the current year using JavaScript?

Originally posted here!

To get the current year, we can use the getFullYear() method from the global Date object in JavaScript.

TL;DR

// Make an instance or object of the Date constructor
const currentDate = new Date();

// get the current year
const year = currentDate.getFullYear();

console.log(year); // 2021
Enter fullscreen mode Exit fullscreen mode

First, let's make an object (or instance) from the global Date constructor using the new keyword like this,

// Make an instance or object of the Date constructor
const currentDate = new Date();
Enter fullscreen mode Exit fullscreen mode

Now we can use the getFullYear() method from the currentDate object to get the year number like this,

// Make an instance or object of the Date constructor
const currentDate = new Date();

// get the current year
const year = currentDate.getFullYear();

console.log(year); // 2021
Enter fullscreen mode Exit fullscreen mode

Yay, We successfully got the year 🎉!

See the above code live in JSBin.

That's all 😃!

Feel free to share if you found this useful 😃.


Discussion (0)