JavaScript Adding seconds to Date object
Last Updated :
11 Jul, 2025
Given a date object and the task is to add seconds to the date using JavaScript. We will use a few methods to add seconds to the date, that are discussed below:
JavaScript getSeconds() Method: This method returns the Seconds(from 0 to 59) of the provided date and time.
Syntax:
Date.getSeconds()
Parameters: This method does not accept any parameters.
Return value: It returns a number, from 0 to 59, representing the seconds.
JavaScript setSeconds() Method: This 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.
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 the new Date with updated second which is set by setSeconds() method.
JavaScript getTime() Method: This method returns the number of milliseconds between midnight of January 1, 1970, and the specified date.
Syntax:
Date.getTime()
Parameters: This method does not accept any parameters.
Return value: It returns a number, representing the number of milliseconds since midnight January 1, 1970.
JavaScript setTime() Method: This 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 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() methods.
JavaScript
let today = new Date();
console.log("Date = " + today);
Date.prototype.addSecs = function (s) {
this.setTime(this.getTime() + (s * 1000));
return this;
}
let a = new Date();
a.addSecs(100);
console.log(a);
Output:
Date = Tue Jun 13 2023 21:33:22 GMT+0530 (India Standard Time)
Date Tue Jun 13 2023 21:35:02 GMT+0530 (India Standard Time)
Example 2: This example adds 10 seconds to the var today by using setSeconds() and getSeconds() methods.
JavaScript
let today = new Date();
console.log("Date = " + today);
Date.prototype.addSecs = function (s) {
this.setSeconds(this.getSeconds() + s);
return this;
}
let a = new Date();
a.addSecs(10);
console.log(a);
Output:
Date = Tue Jun 13 2023 21:35:45 GMT+0530 (India Standard Time)
Date Tue Jun 13 2023 21:35:55 GMT+0530 (India Standard Time)
We have a complete list of JavaScript Date Objects, to check those please go through this JavaScript Date Object Complete reference article.
Similar Reads
JavaScript Adding hours to the Date object Given a date, the task is to add hours to it. To add hours to date in javascript, we're going to discuss a few techniques. First few methods to know. JavaScript getHours() Method: This method returns the hour (from 0 to 23) of the provided date and time. Syntax: Date.getHours() Parameters: This meth
3 min read
JavaScript Adding minutes to Date object Given a date and the task is to add minutes to it using JavaScript. To add minutes to the date object, some methods are used which are listed below: JavaScript getMinutes() Method: This method returns the Minutes (from 0 to 59) of the provided date and time. Syntax: Date.getMinutes() Parameters: Thi
3 min read
JavaScript Adding days in milliseconds to Date object Given a date, the task is to add days in milliseconds to it using JavaScript. To add days in milliseconds to date objects in JavaScript, some methods are used which are listed below: JavaScript getMilliseconds() Method: This method returns the milliseconds (from 0 to 999) of the provided date and ti
2 min read
JavaScript Date setSeconds() Method The date.setSeconds() method is used to set seconds into a Date object which is created using Date() constructor. Syntax: DateObj.setSeconds(seconds_Value) Parameter: This method accepts a single parameter as mentioned above and described below: seconds_Value: This parameter holds the value of secon
4 min read
JavaScript Date setUTCSeconds() Method The date.setUTCSeconds() method is used to set seconds according to universal time into a date object which is created using the Date() constructor. Syntax: DateObj.setUTCSeconds(seconds_Value); Parameter: These methods accept a single parameter as mentioned above and described below: seconds_Value:
4 min read
JavaScript Date UTC() Method In JavaScript, the Date.UTC() method is used to create a date object representing a specified date and time in UTC (Coordinated Universal Time). It accepts the year, month, day, hour, minute, second, and millisecond components of the date and returns the number of milliseconds since January 1, 1970,
4 min read