To get yesterday’s date in JavaScript, call the Date()
constructor to get the current or today’s date and then simply subtract one day from today’s date object using the setDate()
method to get yesterday’s date.
For example,
// Today's date const date = new Date(); // Before console.log(date); // Output: 👉 Mon Mar 20 2023 12:43:56 GMT+0530 (India Standard Time) // Get the day of the month from today's date const day = date.getDate(); // Subtract one day from today's date date.setDate(day - 1); // After subtracting one day console.log(date); // Output: 👉 Sun Mar 19 2023 12:43:56 GMT+0530 (India Standard Time)
The getDate()
method returns an integer between 1 and 31 representing the day of the month for the specified date according to the local time zone.
When you call the getDate()
method on the current date time object, it returns the today’s date.
For example,
// Today's date(Mar 20, 2023) const date = new Date(); console.log(date.getDate()); // Output: 👉 20
On the other hand, the setDate()
method sets the day of the month for a given date object.
For example,
// Today's date(Mar 20, 2023) const date = new Date(); // Set date to 10th Mar date.setDate(10); console.log(date); // Output: 👉 Fri Mar 10 2023 12:59:45 GMT+0530 (India Standard Time)
While using the setDate()
method, you don’t have to care about the zero or negative values.
If you pass a value that is not between 1 and 31, the setDate()
method automatically adjusts the date to a valid date.
For example, if you pass 0 to the setDate()
method as an argument, it automatically sets the date to the last day of the previous month.
For example,
// March 1st, 2023 const date = new Date('2023-03-01'); // Auto adjusts the date to previous month date.setDate(0); console.log(date); // Output: 👉 Tue Feb 28 2023 05:30:00 GMT+0530 (India Standard Time)
2. Using the Timestamp to Get Yesterday’s Date
An alternative approach could be to use the timestamp to get yesterday’s date.
Timestamp is defined as the total number of seconds elapsed since January 1st, 1970.
In this method, we first get the timestamp(in milliseconds) for today’s date by calling the getTime()
method on today’s date object and then subtract the total number of milliseconds in a day from today’s timestamp to get yesterday’s timestamp.
Then, to get the date object from the timestamp, we pass the timestamp to the Date()
constructor as an argument.
For example,
// Today's date(Mar 20, 2023) const date = new Date() // Get today's timestamp(in milliseconds) const timestamp = date.getTime(); // Get yesterday's date const yesterday = new Date(timestamp - 24*60*60*1000); console.log(yesterday); // Output: 👉 Sun Mar 19 2023 13:42:35 GMT+0530 (India Standard Time)
The getTime()
method returns the timestamp in milliseconds for the given date object.
If you call the getTime()
method on the current date object, it returns the current timestamp.
For example,
// Today's date const date = new Date() console.log(date.getTime()); // Output: 👉 1679300210685
You can use either of the two methods to get yesterday’s date in JavaScript. Both methods give the same result.
That’s all for this article. Thanks for reading!