Skip to main content

How to Get Yesterday's Date in JavaScript

Calculating "yesterday's" date is a very common requirement in programming, whether for setting a default date in a report, querying data from the previous day, or simply displaying it in a UI. JavaScript's Date object makes this task simple, but it's important to choose the right method based on your specific needs.

This guide will teach you the standard methods for getting the date of the previous day. You will learn how to get the date while preserving the exact time, and how to get the calendar date for yesterday set to midnight.

The Core Logic: Using setDate()

The key to date arithmetic in JavaScript is the setDate() method. It modifies a Date object by setting the day of the month. Crucially, it's smart enough to automatically "roll back" the month or year if necessary. For example, if you subtract 1 from the 1st of March, it will correctly calculate the last day of February.

note

Best Practice: The setDate() method mutates the Date object it's called on. To avoid unexpected side effects, you should always clone a date before modifying it.

const today = new Date();
const dateToModify = new Date(today.getTime()); // Creates a safe clone

Scenario 1: Getting Yesterday's Date (Preserving the Time)

This is the most common requirement. You want to find out the exact date and time it was 24 hours ago.

For example, we have the current date and time, and we want to find the date and time from exactly one day prior.

// Problem: What was the date and time 24 hours before now?
const now = new Date(); // e.g., 2025-10-16 10:30:00

Recommended Solution: this function subtracts one day from the provided date while keeping the time components intact.

function getYesterday(date) {
// Create a clone to avoid mutating the original date
const yesterday = new Date(date.getTime());
yesterday.setDate(date.getDate() - 1);
return yesterday;
}

// Example Usage:
const now = new Date();
const yesterday = getYesterday(now);

console.log('Now:', now);
// Output: Now: Thu Oct 16 2025 10:30:00 GMT...
console.log('Yesterday:', yesterday);
// Output: Yesterday Wed Oct 15 2025 10:30:00 GMT...
note

This is the most direct and idiomatic way to perform date subtraction.

Scenario 2: Getting the Start of Yesterday (at Midnight)

Sometimes, you only care about the calendar date and want the time to be set to the very beginning of that day (00:00:00). This is useful for date-range comparisons where the time of day is irrelevant.

For example, we want to get the date for yesterday, but we want the time to be set to midnight.

// Problem: What was the calendar date for yesterday, ignoring the current time?
const now = new Date(); // e.g., 2023-10-27 10:30:00

Solution: this can be achieved by creating a new Date object and passing the calculated day component.

function getStartOfYesterday() {
const today = new Date();

return new Date(
today.getFullYear(),
today.getMonth(),
today.getDate() - 1
);
}

// Example Usage:
const yesterdayAtMidnight = getStartOfYesterday();

console.log('Now:', new Date());
// Output: Fri Oct 27 2023 10:30:00 GMT...
console.log('Start of Yesterday:', yesterdayAtMidnight);
// Output: Thu Oct 26 2023 00:00:00 GMT...

Once you have a Date object for yesterday, you can easily format it into any string format you need.

This example combines the logic from the previous sections to produce a formatted string.

function getYesterdayFormatted() {
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);

const year = yesterday.getFullYear();
// Remember to add 1 to the zero-based month
const month = String(yesterday.getMonth() + 1).padStart(2, '0');
const day = String(yesterday.getDate()).padStart(2, '0');

return `${year}-${month}-${day}`;
}

// Example Usage:
const formattedYesterday = getYesterdayFormatted();
console.log(formattedYesterday); // Output: "2025-10-15" (if today is Oct 16)

Conclusion

Getting yesterday's date in JavaScript is a simple task if you choose the right method for your goal.

  • If you need to know the exact date and time relative to the current time, use the setDate(date.getDate() - 1) method on a cloned date. This is the most common requirement.
  • If you only care about the calendar date and want the time to be reset to midnight, create a new Date() using the calculated day component.

By understanding this key distinction, you can choose the right tool for any date subtraction task.