Skip to main content

How to Get Last Week's Date in JavaScript

Calculating a date from the past, such as "one week ago," is a common requirement for setting default date ranges, finding recent data, or performing date-based comparisons. JavaScript's Date object makes this task simple, but it's important to choose the right method based on whether you want to preserve the time of day or just get the calendar date.

This guide will teach you the two standard methods for getting last week's date: one that preserves the exact time and one that gives you the calendar date set to midnight.

The Core Logic: Modifying a Date Object

The key to date arithmetic in JavaScript is to manipulate a Date object. We can subtract days from a date using the setDate() method. This method is smart enough to automatically "roll back" the month or year if necessary (e.g., subtracting 7 days from March 5th will correctly result in a date in February).

note

Best Practice: To avoid unexpected side effects, you should always clone a date before modifying it. This prevents you from changing the original Date object.

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

Scenario 1: Getting the Date a Week Ago (Preserving the Time)

This is the most common requirement. You want to find out the exact date and time it was 7 days ago. The setDate() method is the cleanest and most readable way to achieve this.

Problem: we have the current date and time, and we need to find the date and time from exactly one week prior.

// Problem: What was the date and time exactly 7 days before now?
const now = new Date(); // e.g., 2023-10-27 10:30:00

Solution: this function subtracts 7 days from the provided date while keeping the time components intact.

function getDateAWeekAgo(date) {
const aWeekAgo = new Date(date.getTime()); // Clone the date
aWeekAgo.setDate(date.getDate() - 7);
return aWeekAgo;
}

// Example Usage:
const now = new Date();
const lastWeek = getDateAWeekAgo(now);

console.log('Now:', now);
// Output: Fri Oct 27 2023 10:30:00 GMT...
console.log('Last Week:', lastWeek);
// Output: Fri Oct 20 2023 10:30:00 GMT...
note

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

Scenario 2: Getting the Calendar Date 7 Days Ago (at Midnight)

Sometimes, you only care about the calendar date and want to ignore the time. In this case, you can create a new Date object by passing its year, month, and day components, which will default the time to 00:00:00 in the local timezone.

Problem: we need to get the date for 7 days ago, but we want the time to be set to the very beginning of that day.

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

Solution:

function getCalendarDateAWeekAgo(date) {
return new Date(
date.getFullYear(),
date.getMonth(),
date.getDate() - 7
);
}

// Example Usage:
const now = new Date();
const lastWeekCalendarDate = getCalendarDateAWeekAgo(now);

console.log('Now:', now);
// Output: Fri Oct 27 2023 10:30:00 GMT...
console.log('Last Week (at midnight):', lastWeekCalendarDate);
// Output: Fri Oct 20 2023 00:00:00 GMT...
note

This method is useful when you need to establish a "start of day" timestamp for date-range comparisons.

Conclusion: Which Method Should You Use?

The correct method depends entirely on your goal.

  • If you need to know the exact date and time relative to the current time (e.g., "what was the time exactly 168 hours ago?"), use the setDate() 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 (e.g., "what day was it 7 days ago?"), 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.