Getting the current Year in JavaScript
In this tutorial, we are going to learn about how to get the current year in JavaScript using the new Date()
constructor.
Getting the current year
We can get the current year in JavaScript by calling a getFullYear()
method on a new Date() constructor.
The
getFullYear()
method returns the current year in four-digit(2020) format according to the user local time.
Here is an example:
const currentYear = new Date().getFullYear();
console.log(currentYear); // 2020
Displaying the current year in Webpage
We can also display the current year in our webpage footer section like this:
Html:
<footer>
<p id="year"></p>
</footer>
JavaScript:
const el = document.getElementById("year");
const currentYear = new Date().getFullYear();
el.innerText = currentYear;