by Sai gowtham

How to get yesterday's date in JavaScript

In this tutorial, we are going to learn about how to get the yesterday’s date using JavaScript.

Getting Yesterday’s date

First, we need to access the today’s date using new Date() constructor then we need to subtract it with 1.

const today = new Date();

// it gives yesterday date
today.setDate(today.getDate()-1);
console.log(today.toDateString()); // Thu Apr 02 2020

In the above code, we used the setDate() method on today and passed today's date minus 1 as arguments to get yesterday’s date.

Note: If today is 1st March, JavaScript can figure out yesterday is 28th February.

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
26,545 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)
221,520 students enrolled
44 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)
114,575 students enrolled
21 hours of video content
View Course