by Sai gowtham

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;

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course