Open In App

How to calculate the yesterday’s date in JavaScript ?

Last Updated : 03 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see how to calculate yesterday’s date in JavaScript. To calculate yesterday’s date, you need to have some basic ideas of a few methods of JavaScript.

JavaScript getDate() Method:

It is an inbuilt JavaScript function that returns the day of the month as a number (1-31).

 Syntax:

 dateObj.getDate() 

JavaScript setDate() Method:

It is an inbuilt JavaScript function used to set the day of the month into a date object. 

Syntax:

dateObj.setDate()

JavaScript getTime() and setTime() Method:

The getTime() method returns the number of milliseconds since the Unix Epoch (January 1, 1970, 00:00:00 UTC). By subtracting the equivalent milliseconds for one day (which is 24 hours * 60 minutes * 60 seconds * 1000 milliseconds), we can set the date to yesterday.

Approach: JavaScript allows us to create a platform-independent date instance that represents a single moment in time using Date constructor. An empty Date constructor creates a new date object that represents the current date and time. The Date constructor can also be specified to create a date object that represents a particular date and time. We use the getDate() function to fetch the current date from the date object and subtract one day from it using the setDate() function which sets yesterday’s date onto the date object.

Example 1: Here we will get yesterday’s date, in this example, we will show the output compared to today’s date. Syntax:

// Empty Date constructor representing current time
let dateObj = new Date();

// Current Time => Wed Jun 12 2019 20:52:24 GMT+0530
// (India Standard Time)
dateobj;

The following programs illustrate the solution

JavaScript
// JavaScript program to illustrate
// calculation of yesterday's date

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

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

console.log(dateObj);

Output
2023-06-13T16:23:58.062Z

Example 2: Here we will get the yesterday date of the pre-defined date which is Fri May 10, 2019, 16:30:00 GMT+0530 (India Standard Time). 

Syntax:

// Specified Date constructor representing
// particular time
let dateObj = new Date(2019, 04, 10, 16, 30, 00);

// Specific Time => Fri May 10 2019 16:30:00 GMT+0530
// (India Standard Time)
dateObj;
JavaScript
// Create a specified date object using
// Date constructor
let dateObj = new Date(2019, 04, 10, 16, 30, 00);

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

console.log(dateObj);

Output
2019-05-09T16:30:00.000Z

Example 3: In this example, we will calculate yesterday’s date based on the current date and time:

Syntax:

// 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 (24 hours * 60 minutes * 60 seconds * 1000 milliseconds)
let yesterdayTime = currentTime - (24 * 60 * 60 * 1000);

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

console.log(dateObj);
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 (24 hours * 60 minutes * 60 seconds * 1000 milliseconds)
let yesterdayTime = currentTime - (24 * 60 * 60 * 1000);

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

console.log(dateObj);

Output
2024-05-31T03:40:36.606Z




Next Article

Similar Reads