How to calculate the date three months prior using JavaScript?

To calculate the date three months prior using JavaScript, we will first need to create a new date object, which will represent the current date. We will then use the setMonth() method to subtract 3 from the current month. Finally, we will convert this new date object back to a string using the toString method to display the date three months prior.

Basically, we will be writing a dynamic JavaScript function that can take in a number as its input and return the date prior to that number of months from today's date.

For example ?

calculatePriorDate(4);

This function should return 19/09/2022 assuming that today's date is 19/01/2023 in DD/MM/YYYY format.

Approach

  • This is a simple functionality and can be achieved using the native JS Date Object.

  • We will be using the JS Date getMonth and setMonth methods to respectively get and set new months.

  • The function responsible for doing this will look like ?

const calculatePriorDate = (priorMonths = 1) => {
   const date = new Date();
   
   // accessing current month of the date
   const currentMonth = date.getMonth();
   
   // subtracting required number of months
   date.setMonth(currentMonth - priorMonths);
   
   return date.toLocaleDateString();
};

This function will simply take in the month gap and return the prior date string after subtracting the required number of months.

Basic Example

Here's a simple Node.js example to demonstrate the functionality:

const calculatePriorDate = (priorMonths = 1) => {
   const date = new Date();
   
   // accessing current month of the date
   const currentMonth = date.getMonth();
   
   // subtracting required number of months
   date.setMonth(currentMonth - priorMonths);
   
   return date.toLocaleDateString();
};

// Calculate dates for different month gaps
console.log("Current date:", new Date().toLocaleDateString());
console.log("1 month prior:", calculatePriorDate(1));
console.log("3 months prior:", calculatePriorDate(3));
console.log("6 months prior:", calculatePriorDate(6));
Current date: 12/15/2023
1 month prior: 11/15/2023
3 months prior: 9/15/2023
6 months prior: 6/15/2023

Interactive Browser Example

The full working code for an interactive web page will be:

<!DOCTYPE html>
<html>
<head>
   <title>Prior Date Calculator</title>
   <style>
      body {
         font-family: Arial, sans-serif;
         padding: 20px;
      }
      #space {
         color: #333;
         font-weight: bold;
         margin-top: 15px;
      }
      input, button {
         padding: 8px;
         margin: 5px;
      }
   </style>
</head>
<body>
   <h2>Calculate Prior Date</h2>
   <input placeholder="Enter number of months..." id="gap" type="number" />
   <button onclick="renderGap()">Calculate</button>
   <p id="space"></p>
   
   <script>       
      const calculatePriorDate = (priorMonths = 1) => {
         const date = new Date();
         
         // accessing current month of the date
         const currentMonth = date.getMonth();
         
         // subtracting required number of months
         date.setMonth(currentMonth - priorMonths);
         
         return date.toLocaleDateString();
      };    
      
      const renderGap = () => {
         const space = document.getElementById('space');
         const gapInput = document.getElementById('gap');
         const gap = parseInt(gapInput.value) || 1;
         const newDate = calculatePriorDate(gap);
         
         space.innerText = `Date ${gap} month(s) prior: ${newDate}`;
      }
      
      // Show current date on page load
      window.onload = () => {
         document.getElementById('space').innerText = `Current date: ${new Date().toLocaleDateString()}`;
      };
   </script>
</body>
</html>

Key Points

  • The setMonth() method automatically handles year rollover when subtracting months

  • JavaScript's getMonth() returns 0-11 (January = 0, December = 11)

  • The function uses a default parameter of 1 month if no argument is provided

  • toLocaleDateString() formats the date according to the user's locale

Conclusion

Using JavaScript's Date object with setMonth() provides a simple way to calculate prior dates. The method automatically handles month and year boundaries, making it reliable for date calculations.

Updated on: 2026-03-15T23:19:00+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements