Increment a given date in JavaScript

17 May, 2019
Given a date, the task is to increment it. To increment a date in javascript, we’re going to discuss few techniques. First few methods to know:
JavaScript getDate() methodThis method returns the day of the month (from 1 to 31) for the defined date.Syntax:Date.getDate()
Return value:It returns a number, from 1 to 31, denoting the day of the month
Date.getDate()

Return value:It returns a number, from 1 to 31, denoting the day of the month
JavaScript setDate() methodThis method sets the day of month to the date object.Syntax:Date.setDate(day)
Parameters:day:This parameter is required. It specifies the integer defining the day of a month. Values expected are 1-31 but less than 1 and greater than 31 values are used appropriately for previous and next month.Return value:It returns, representing the number of milliseconds between the date object and midnight January 1 1970.
Date.setDate(day)

Parameters:
day:This parameter is required. It specifies the integer defining the day of a month. Values expected are 1-31 but less than 1 and greater than 31 values are used appropriately for previous and next month.
Return value:It returns, representing the number of milliseconds between the date object and midnight January 1 1970.
JavaScript getTime() methodThis method returns the number of milliseconds between midnight of January 1, 1970 and the specified date.Syntax:Date.getTime()
Return value:It returns a number, representing the number of milliseconds since midnight January 1, 1970.
Date.getTime()

Return value:It returns a number, representing the number of milliseconds since midnight January 1, 1970.
JavaScript setTime() methodThis method set the date and time by adding/subtracting a defines number of milliseconds to/from midnight January 1, 1970.Syntax:Date.setTime(millisec)
Parameters:millisec:This parameter is required. It specifies the number of milliseconds to be added/subtracted, midnight January 1, 1970Return value:It returns, representing the number of milliseconds between the date object and midnight January 1 1970.
JavaScript setTime() methodThis method set the date and time by adding/subtracting a defines number of milliseconds to/from midnight January 1, 1970.Syntax:
Date.setTime(millisec)

Parameters:
millisec:This parameter is required. It specifies the number of milliseconds to be added/subtracted, midnight January 1, 1970
Return value:It returns, representing the number of milliseconds between the date object and midnight January 1 1970.
Example 1:This example increments 1 day to the 16th may by using setDate() and getDate() method.
<!DOCTYPE html><html>  <head>    <title>        JavaScript       | Incrementing a date.    </title></head>  <body style="text-align:center;" id="body">    <h1 style="color:green;">              GeeksForGeeks          </h1>    <p id="GFG_UP"       style="font-size: 15px;              font-weight: bold;">    </p>    <button onclick="gfg_Run()">        Increment by 1 day    </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;          function gfg_Run() {            var tomorrow = new Date();            tomorrow.setDate(today.getDate() + 1);            el_down.innerHTML = tomorrow;        }    </script></body>  </html>
Output:
Before clicking on the button:
After clicking on the button:
Example 2:This example increments 10 day to the 16th may by using setTime() and getTime() method.
<!DOCTYPE html><html>  <head>    <title>        JavaScript | Incrementing a date.    </title></head>  <body style="text-align:center;"      id="body">    <h1 style="color:green;">              GeeksForGeeks          </h1>    <p id="GFG_UP"        style="font-size: 15px;               font-weight: bold;">    </p>    <button onclick="gfg_Run()">        Increment by days    </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();        var days = 10;        el_up.innerHTML = "Today's date = " + today;          function gfg_Run() {            var tomorrow = new Date();            tomorrow.setTime(today.getTime() + days * 86400000);            el_down.innerHTML = tomorrow;        }    </script></body>  </html>
Output:
Before clicking on the button:
After clicking on the button:
javascript-date
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.