How to calculate the date three months prior using JavaScript ?

Given a Date and the task is to get the date of three month 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 return the date.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html
    <head
        <title
            How to calculate the date
            three months prior to today
        </title>
    </head
      
    <body style = "text-align:center;"
          
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1>
          
        <p id = "GFG_UP" style =
            "font-size: 19px; font-weight: bold;">
        </p>
          
        <button onClick = "GFG_Fun()">
            click here
        </button>
          
        <p id = "GFG_DOWN" style =
            "color: green; font-size: 24px; font-weight: bold;">
        </p>
          
        <script>
            var up = document.getElementById('GFG_UP');
            var down = document.getElementById('GFG_DOWN');
            var d = new Date();
              
            up.innerHTML = "Today's Date= "+ d.toLocaleDateString();
              
            function GFG_Fun() {
                d.setMonth(d.getMonth() - 3);
                down.innerHTML = "Before 3 Months, Date is "
                        + d.toLocaleDateString();
            }
        </script
    </body
</html>                    

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html
    <head
        <title
            How to calculate the date
            three months prior to today
        </title>
    </head
      
    <body style = "text-align:center;"
          
        <h1 style = "color:green;"
            GeeksForGeeks 
        </h1>
          
        <p id = "GFG_UP" style =
            "font-size: 19px; font-weight: bold;">
        </p>
          
        <button onClick = "GFG_Fun()">
            click here
        </button>
          
        <p id = "GFG_DOWN" style =
            "color: green; font-size: 24px; font-weight: bold;">
        </p>
          
        <script>
            var up = document.getElementById('GFG_UP');
            var down = document.getElementById('GFG_DOWN');
            var d = new Date("2010/12/02");
              
            up.innerHTML = "Date= "+ d.toLocaleDateString();
              
            function GFG_Fun() {
                d.setMonth(d.getMonth() - 3);
                down.innerHTML = "Before 3 Months, Date is "
                        + d.toLocaleDateString();
            }
        </script
    </body
</html>                    

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.