Calculate the yesterday's date in JavaScript

Last Updated : 2 Sep, 2026

Calculating yesterday’s date in JavaScript can be done using the Date object and its date manipulation methods. The most common approach is to subtract one day from the current or specified date.

  • Create a Date object representing the current or specified date.
  • Use getDate() to retrieve the day of the month.
  • Subtract one day using setDate() or subtract one day in milliseconds using getTime() and setTime().

Approach 1: Using getDate() and setDate() Methods

The getDate() method returns the day of the month, while setDate() updates the day of the month. By subtracting 1 from the value returned by getDate(), we can calculate yesterday's date.

Example 1: In this example, we calculate yesterday's date based on the current date and time.

JavaScript
// Create a date object using Date constructor
let dateObj = new Date();

// Subtract one day from current date
dateObj.setDate(dateObj.getDate() - 1);

console.log(dateObj);

Example 2: In this example, we calculate yesterday's date based on a predefined date.

JavaScript
// Create a specified date object
let dateObj = new Date(2019, 4, 10, 16, 30, 0);

// Subtract one day from specified date
dateObj.setDate(dateObj.getDate() - 1);

console.log(dateObj);

Approach 2: Using getTime() and setTime() Methods

The getTime() method returns the number of milliseconds since the Unix Epoch. Since one day is equivalent to 24 * 60 * 60 * 1000 milliseconds, subtracting this value gives the timestamp for the previous day. The setTime() method then updates the Date object.

Example: In this example, we calculate yesterday's date by subtracting one day's worth of milliseconds from the current time.

JavaScript
// Create a date object using Date constructor
let dateObj = new Date();

// Get the current time in milliseconds
let currentTime = dateObj.getTime();

// Subtract one day in milliseconds
let yesterdayTime = currentTime - (24 * 60 * 60 * 1000);

// Set the time back to the date object
dateObj.setTime(yesterdayTime);

console.log(dateObj);
Comment