Open In App

How to calculate the date three months prior using JavaScript ?

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

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






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. 






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
Article Tags :