Open In App

How to calculate the date three months prior using JavaScript ?

Last Updated : 27 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

To calculate the date three months prior using JavaScript, we could use the getMonth() and setMonth() methods. In this article, we are going to learn how to calculate the date three months prior using JavaScript.

Approach

  • First, select the date object.
  • Then use the getMonth() method to get the months.
  • Then subtract three months from the getMonth() method and set it using setMonth() method.

Example 1: This example uses getMonth() and setMonth() methods to get and set the month date. 

Javascript




let d = new Date();
 
console.log("Today's Date: "
    + d.toLocaleDateString());
 
d.setMonth(d.getMonth() - 3);
 
console.log("3 months Prior Date: "
        + d.toLocaleDateString());


Output

Today's Date: 12/27/2023
3 months Prior Date: 9/27/2023

Example 2: This example uses getMonth() and setMonth() methods to get and set the month date as provided. 

Javascript




let d = new Date("2018/12/02");
 
console.log("Date: " + d.toLocaleDateString());
 
d.setMonth(d.getMonth() - 3);
console.log("3 Months Prior Date: "
        + d.toLocaleDateString());


Output

Date: 12/2/2018
3 Months Prior Date: 9/2/2018

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads