JavaScript | Adding seconds to Date object

23 May, 2019
Given a date, the task is to add seconds to it. To add seconds to date in javascript, we’re going to discuss a few techniques. First few methods to know.
JavaScript getSeconds() MethodThis method returns the Seconds(from 0 to 59) of the provided date and time.Syntax:Date.getSeconds()
Return value:It returns a number, from 0 to 59, representing the seconds.
Date.getSeconds()

Return value:It returns a number, from 0 to 59, representing the seconds.
JavaScript setSeconds() MethodThis method set the seconds of a date object.This method can also be used to set the milliseconds.Syntax:Date.setSeconds(sec, millisec)
Parameters:sec: This parameter is optional. It specifies the integer representing the seconds. Values expected are 0-59, but other values are allowed.millisec: This parameter is optional. It specifies the integer representing the milliseconds. Values expected are 0-999, but other values are allowed.Note:All the previous 2 parameters accept values apart from their range and these values adjust like.sec = -1, means the last second of the previous minute and same for the other parameters.if sec passed is 60, means the first second of the next minute and same for the other parameters.Return value:It returns a number, denoting the number of milliseconds between the date object and midnight January 1, 1970.
JavaScript setSeconds() MethodThis method set the seconds of a date object.This method can also be used to set the milliseconds.Syntax:
Date.setSeconds(sec, millisec)

Parameters:
sec: This parameter is optional. It specifies the integer representing the seconds. Values expected are 0-59, but other values are allowed.
millisec: This parameter is optional. It specifies the integer representing the milliseconds. Values expected are 0-999, but other values are allowed.
Note:All the previous 2 parameters accept values apart from their range and these values adjust like.
sec = -1, means the last second of the previous minute and same for the other parameters.
if sec passed is 60, means the first second of the next minute and same for the other parameters.
Return value:It returns a number, denoting 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 defined 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 defined 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 adds 100 seconds to the var today by using setTime() and getTime() method.
<!DOCTYPE HTML><html>  <head>    <title>        JavaScript       | Adding Seconds to Date object.    </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()">        addSeconds    </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 = "Date = " + today;        Date.prototype.addSecs = function(s) {            this.setTime(this.getTime() + (s * 1000));            return this;        }          function gfg_Run() {            var a = new Date();            a.addSecs(100);            el_down.innerHTML = a;        }    </script></body>  </html>
Output:
Before clicking on the button:
After clicking on the button:
Example 2: This example adds 10 seconds to the var today by using setSeconds() and getSeconds() method.
<!DOCTYPE HTML><html>  <head>    <title>        JavaScript       | Adding Seconds to Date object.    </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()">        addSeconds    </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 = "Date = " + today;        Date.prototype.addSecs = function(s) {            this.setSeconds(this.getSeconds() + s);            return this;        }          function gfg_Run() {            var a = new Date();            a.addSecs(10);            el_down.innerHTML = a;        }    </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.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
How to append HTML code to a div using JavaScript ?
Difference Between PUT and PATCH Request
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?