How to calculate the yesterday’s date in JavaScript ?
Last Updated :
03 Jun, 2024
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);
Output2023-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);
Output2019-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);
Output2024-05-31T03:40:36.606Z
Similar Reads
How to get tomorrow's date in a string format in JavaScript ?
In this article, we will see how to print tomorrow's date in string representation using JavaScript. To achieve this, we use the Date object and create an instance of it. After that by using the setDate() method, we increase one date to the present date. Now by using the getDate() method you will ge
2 min read
How to check for two timestamp for the same day?
Given two timestamp then we have to find out whether these time are of same date or not. Here we can use the JavaScript Date Object. Examples: Input: TimeStamp1 = 20-04-2020 , 16:04:55 and TimeStamp2 = 20-04-2020 , 10:22:42 Output: These dates are of same dateInput: TimeStamp1 = 20-04-2020 , 16:04:5
3 min read
How to get seconds since epoch in JavaScript?
Given a date, we have to find the number of seconds since the epoch (i.e. 1 January 1970, 00:00:00 UTC ). The getTime() method in the JavaScript returns the number of milliseconds since January 1, 1970, or epoch. If we divide these milliseconds by 1000 and then integer part will give us the number o
1 min read
How to check if one date is between two dates in JavaScript ?
The task is to determine if the given date is in between the given 2 dates or not? Here are a few of the most used techniques discussed with the help of JavaScript. In the first approach, we will use .split() method and the new Date() constructor. And in the second approach we will use the .getTime(
3 min read
How to check if the given date is weekend ?
To check if a given date falls on a weekend in JavaScript, you can use the getDay() method on a Date object. This method returns a number representing the day of the week, where 0 is Sunday and 6 is Saturday. There are two methods to solve this problem which are discussed below: Table of Content Usi
2 min read
How to Convert Date to Another Timezone in JavaScript?
Converting a date to another timezone in JavaScript means adjusting the local date and time to match the time in a different timezone. This ensures that the displayed time aligns with the selected timezone, often using built-in methods or external libraries. 1. Using Intl.DateTimeFormat() and format
2 min read
How to check if date is less than 1 hour ago using JavaScript ?
Given a date and the task is to check whether the given date is less than 1 hour ago or not with the help of JavaScript. Approach 1: Count the milliseconds of the difference between the current and prev_date.If those are greater than milliseconds in 1 hour, then it returns false otherwise returns tr
2 min read
How to check a date is valid or not using JavaScript?
To check if a date is valid or not in JavaScript, we have to know all the valid formats of the date. For ex - "YYYY/DD/MM", "DD/MM/YYYY", and "YYYY-MM-DD", etc. We have a given date format and we need to check whether the given format is valid or not according to the official and acceptable date for
3 min read
How to get the day and month of a year using JavaScript ?
Given a date and the task is to get the day and month of a year using JavaScript. Approach: First get the current date by using new Date().Use getDay() method to get the current day in number format, Map it to the day name.Use getMonth() to get the current month in number format, Map it to the month
2 min read
How to calculate the date three months prior using JavaScript ?
To calculate the date three months prior using JavaScript, we could use the getMonth() and setMonth() methods. In this article, we are going to learn how to calculate the date three months prior using JavaScript. ApproachFirst, select the date object.Then use the getMonth() method to get the months.
1 min read