by Sai gowtham

How to get the previous year in JavaScript

In this tutorial, we are going to learn about how to get the previous year from the current date using JavaScript.

Getting the previous year

To get the previous year in JavaScript, first we need to access the current year by calling a getFullYear() method on new Date() constructor and subtract it with -1.

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(); // 2020

const previousYear =  currentYear-1;

console.log(previousYear); // 2019

If you want to set a previous year to the current date, you can do it like this:

const d = new Date();
d.setFullYear(d.getFullYear()-1);

console.log(d.toDateString()); // "Fri Oct 04 2019"

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