Wednesday, November 19, 2025
HomeLanguagesJavascriptHow to calculate the date three months prior using JavaScript ?

How to calculate the date three months prior using JavaScript ?

Given a Date or current date, and the task is to get 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: 6/12/2023
3 months Prior Date: 3/12/2023

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

Javascript




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


Output

Date: 12/2/2010
3 Months Prior Date: 9/2/2010
RELATED ARTICLES

Most Popular

Dominic
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6775 POSTS0 COMMENTS
Nicole Veronica
11924 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6903 POSTS0 COMMENTS
Ted Musemwa
7159 POSTS0 COMMENTS
Thapelo Manthata
6859 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS