Home Node.js How to get yesterday’s date in JavaScript

How to get yesterday’s date in JavaScript

In this article we will show you how to get yesterdays date in javascript

The first  step is to get the current date and subtract 1 from it and use the function setDate().

Example

var currentDate = new Date();
console.log("The current date="+currentDate);
var yesterdayDate = currentDate.setDate(currentDate.getDate()- 1);
console.log("Yesterdays date ="+new Date(yesterdayDate));

Either save this as currentdate.js and run line this at a command line or open a developer console window in a brower like Brave, firefox or chrome

node currentdate.js

When I ran this example I saw the following

The current date=Sun Apr 10 2022 13:08:17 GMT+0100 (British Summer Time)
Yesterdays date = Sat Apr 09 2022 13:08:17 GMT+0100 (British Summer Time)

You may also like