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.