Javascript date subtract days

A date is given and the task is to subtract days from the date. To subtract days from a date in JavaScript, you use some of the methods described below: JavaScript getDate () Method: This method returns the day of the month (from 1 to 31) for a specific date. Syntax: Returned value: returns a…

A date is given and the task is to subtract days from the date. To subtract days from a date in JavaScript, you use some of the methods described below:

JavaScript getDate () Method: This method returns the day of the month (from 1 to 31) for a specific date.

Syntax:

Date.getDate()


Returned value: returns a number between 1 and 31, representing the day of the month.

JavaScript setDate () Method: This method sets the day of the month for the object’s date.

Syntax:

Date.setDate(day)


Parameters:

  • day: this is a required parameter. Specifies an integer representing the day of the month. Values of the expected values are: 1-31, but other values are allowed.
  • 0 will result in the last day of the previous month.
  • -1 will result in the day before the last day of the previous month.
  • If a month has 31 days, 32 will result in the first day of the next month.
  • If a month has 30 days, 32 will result in the second day of the next month.

Return value: returns a number representing the number of milliseconds between the object’s date and midnight on January 1, 1970.

JavaScript getTime () method: this method returns the number of milliseconds between midnight on January 1, 1970, and the specified date.


Syntax:

Date.getTime().


Return value: Returns a number representing the number of milliseconds since midnight on January 1, 1970.

JavaScript setTime () method: this method sets the date and time by adding / subtracting a specific number of milliseconds to / from midnight on January 1, 1970.

Syntax:

Date.setTime(millisec)


Parameters:

  • millisec: This is a required parameter. It specifies the number of milliseconds to be added / subtracted at midnight on January 1, 1970.


Return value: represents the number of milliseconds between the object date and midnight of January 1, 1970.

Example 1:

In this example, subtract 4 days from the today variable using the setTime () and getTime () methods .

<!DOCTYPE HTML> 

<html> 

    <head> 

        <title> 

            Subtract days from Date object

        </title>

    </head> 

      

    <body style = "text-align:center;"> 

      

        <h1 style = "color:green;" > 

            GeeksForGeeks 

        </h1>

          

        <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;">

        </p>

          

        <button onclick = "gfg_Run()"> 

            subtractDays

        </button>

          

        <p id = "GFG_DOWN" style = 

            "color:green; font-size: 20px; font-weight: bold;">

        </p>

          

        <script>

            var el_up = document.getElementById("GFG_UP");

            var el_down = document.getElementById("GFG_DOWN");

            var today = new Date();

            el_up.innerHTML = "Today's date = " + today;

              

            Date.prototype.subtractDays = function(d) { 

                this.setTime(this.getTime() - (d*24*60*60*1000)); 

                return this; 

            }

              

            function gfg_Run() {

                var a = new Date();

                a.subtractDays(4);

                el_down.innerHTML = a;

            }         

        </script> 

    </body> 

</html>                    

Result:

  • Before you press the button:
  • After pressing the button:

Example 2:

In this example, subtract 365 days from the today variable using the setDate () and getDate () methods.

<!DOCTYPE HTML> 

<html> 

    <head> 

        <title> 

            Subtract days from Date object

        </title>

    </head> 

      

    <body style = "text-align:center;">

           

        <h1 style = "color:green;" > 

            GeeksForGeeks 

        </h1>

          

        <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;">

        </p>

          

        <button onclick = "gfg_Run()"> 

            subtractDays

        </button>

          

        <p id = "GFG_DOWN" style = 

            "color:green; font-size: 20px; font-weight: bold;">

        </p>

          

        <script>

            var el_up = document.getElementById("GFG_UP");

            var el_down = document.getElementById("GFG_DOWN");

            var today = new Date();

            el_up.innerHTML = "Today's date = " + today;

              

            Date.prototype.subtractDays= function(d) {

                this.setDate(this.getDate() - d);

                return this;

            }

              

            function gfg_Run() {

                var a = new Date();

                a.subtractDays(365);

                el_down.innerHTML = a;

            }         

        </script> 

    </body> 

</html>                    

Result:

  • Before you press the button:
  • After pressing the button:

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *