How to get yesterday date in JavaScript

Last updated on Sep 24, 2022 by Suraj Sharma



In this tutorial, you'll learn how to get yesterday date and using vanilla JavaScript Date object.



Get Yesterday's Date


To get yesterday date, we will follow the following steps:


1) Get the current timestamp in milliseconds using Date object getTime() method,


const timeStamp = new Date().getTime();

2) Convert 24 hours to milliseconds and subtract it from the current timestamp.,


const yesterdayTimeStamp = timeStamp - 24*60*60*1000;

3) Finally, initialize new Date() constructor with yesterdayTimeStamp


const yesterdayDate = new Date(yesterdayTimeStamp);


Here's the complete code in Vanilla JavaScript


function getYesterdayDate() {
  return new Date(new Date().getTime() - 24*60*60*1000);
}

console.log(getYesterdayDate());


Related Solutions


Rate this post


Suraj Sharma is a Full Stack Software Engineer. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.