The setSeconds method sets the seconds value of the point in time stored in a Date object.
Syntax
The setSeconds method has two overloads:
myDate.setSeconds( seconds )
myDate.setSeconds( seconds, milliseconds )
myDate.setSeconds( seconds, milliseconds )
With:
- seconds being a required parameter that consists of an integer number that, when it is between 0 and 59, changes only the seconds value of the point in time stored in a Date object. Any value outside that range increases or decreases the point in time of a Date object in a quantity equal to the range's limit being exceeded plus the remaining seconds.
- milliseconds being an optional parameter that consists of an integer number that, when it is between 0 and 999, changes only the milliseconds value of the point in time stored in a Date object. Any value outside that range increases or decreases the point in time of a Date object in a quantity equal to the range's limit being exceeded plus the remaining milliseconds.
- myDate being an object based on the Date object.
Instead of being instantiated from a class, in JavaScript objects are created by replicating other objects. This paradigm is called Prototype-based programming.
Example
The following code uses the setSeconds method to set the seconds value of the current time to 30:
<script type="text/javascript">
var currentDate = new Date();
var outcome = "Current time is: <br /><br/>" + currentDate.toTimeString();
outcome += "<br/><br/>";
currentDate.setSeconds(30);
outcome += "After changing its seconds value to 30, the time is: <br /><br />";
outcome += currentDate.toTimeString();
document.write(outcome);
</script>
var currentDate = new Date();
var outcome = "Current time is: <br /><br/>" + currentDate.toTimeString();
outcome += "<br/><br/>";
currentDate.setSeconds(30);
outcome += "After changing its seconds value to 30, the time is: <br /><br />";
outcome += currentDate.toTimeString();
document.write(outcome);
</script>
Technical Details
The setSeconds method belongs to the Date object, it is available since the version 1.0 of JavaScript, and it is supported in all major browsers.
JavaScript Manual >> Date Object >> setSeconds Method