The setSeconds method in JavaScript is utilized to assign the seconds of a particular date according to local time.
Syntax
The syntax for the setSeconds method is expressed as follows:
dateObj.setSeconds( secValue[, msValue]]])
Parameter
secValue - This denotes an integer ranging from 0 to 59, indicating the second. Should the supplied second value exceed 59, the setSeconds function will adjust the minute value appropriately.
msValue - This parameter is not mandatory. It denotes an integer within the range of 0 to 999, indicating the millisecond component. In cases where the supplied millisecond value exceeds 999, the setSeconds function will adjust the seconds value accordingly.
JavaScript Date setSeconds method example
In this section, we will explore the setSeconds method by examining a variety of examples.
Example 1
Let’s examine an example that demonstrates how to display both the current second value and the updated second value.
<script>
var seconds=new Date();
document.writeln("Current Second : "+seconds.getSeconds()+"<br>");
seconds.setSeconds(15);
document.writeln("Updated Second : "+seconds.getSeconds());
</script>
Output:
Current Second : 2
Updated Second : 15
Example 2
Consider an example where we aim to modify the second value of the specified time.
<script>
var seconds=new Date("August 15, 1947 20:22:10");
seconds.setSeconds(15);
document.writeln("Updated Second : "+seconds.getSeconds());
</script>
Output:
Updated Second : 15
Example 3
In this illustration, we will define a millisecond value that exceeds 999, along with the seconds component.
<script>
var seconds=new Date("August 15, 1947 20:22:10");
seconds.setSeconds(10,1012);
document.writeln("Updated Second : "+seconds.getSeconds());
</script>
Output:
Updated Second : 11