How to Get the Last 3 Months’ Dates in JavaScript

In JavaScript, getting the dates for the last three months from the current date is a useful operation, especially for tasks like generating reports, analyzing trends, or displaying historical data. This guide will show you how to calculate these dates efficiently.

let currentDate = new Date();
let threeMonthsAgo = new Date(currentDate.setMonth(currentDate.getMonth() - 3));
console.log(threeMonthsAgo);

To get the date three months ago from the current date in JavaScript, use the setMonth() method:

Calculating the date for the last three months is common in scenarios where you need to track changes or analyze data over time. JavaScript provides built-in methods that make these calculations straightforward and reliable.

Using setMonth() Method

The setMonth() method allows you to modify the month of a Date object. By subtracting 3 from the current month, you can easily calculate the date three months ago.

Example: Getting the Date Three Months Ago

Here’s how you can get the date exactly three months before the current date:

let currentDate = new Date();
let threeMonthsAgo = new Date(currentDate.setMonth(currentDate.getMonth() - 3));
console.log(threeMonthsAgo);

Explanation:

  • new Date(): Creates a new Date object representing the current date and time.
  • currentDate.getMonth(): Retrieves the current month as an integer (0-11).
  • currentDate.setMonth(currentDate.getMonth() - 3): Subtracts 3 from the current month and updates the Date object.
  • new Date(): Wraps the result in a new Date object to ensure correct date handling.
  • console.log(threeMonthsAgo): Prints the date from three months ago.

Iterating Over the Last 3 Months

If you need to get the dates for each of the last three months, you can loop through them as shown below:

let currentDate = new Date();

for (let i = 1; i <= 3; i++) {
    let pastDate = new Date(currentDate.setMonth(currentDate.getMonth() - 1));
    console.log(`Month ${i}: ${pastDate}`);
}

Explanation:

  • for (let i = 1; i <= 3; i++): Loops three times, once for each of the last three months.
  • currentDate.setMonth(currentDate.getMonth() - 1): Subtracts one month each time the loop runs.
  • console.log(): Prints the date for each of the last three months.

Handling Edge Cases

When working with dates, it’s important to consider edge cases, such as:

  • Month Boundaries: JavaScript automatically handles month boundaries, such as moving from January to December.
  • Day Overflow: If the current day doesn’t exist in the target month (e.g., moving from March 31 to February), JavaScript adjusts the date automatically.

Example:

let date = new Date('2024-03-31');
date.setMonth(date.getMonth() - 3);
console.log(date); // 2023-12-31 (Correctly Adjusts for Month Length)

Conclusion

Getting the dates for the last three months in JavaScript is a straightforward task using the setMonth() method. Whether you need just the date from three months ago or an array of dates for each of the last three months, JavaScript provides the tools to perform these calculations efficiently. Understanding these methods allows you to handle date manipulation in your projects with confidence, ensuring accurate and reliable results.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top