How to get the current year using JavaScript?
Published August 18, 2021
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
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();
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
Yay, We successfully got the year 🎉!
See the above code live in JSBin.
That's all 😃!