Home »
JavaScript
Date setSeconds() method with example in JavaScript
JavaScript Date setSeconds() method: Here, we are going to learn about the setSeconds() method in JavaScript with example.
Submitted by IncludeHelp, on March 13, 2019
JavaScript Date setSeconds() method
setSeconds() method is a Date class method, it is used to set the seconds to the Date object with a valid second value (between 00 to 59).
Note: Seconds value larger than the 59 will be truncated from the starting seconds value (00), for example, we are going to set 61 as a second value, it will be truncated as 1.
Syntax
var dt = new Date();
dt.setSeconds(second);
Examples:
Input/Date class object declaration:
var dt = new Date();
Function call to set the Seconds:
dt.setSeconds(45);
Function call to get the Seconds:
dt.getSeconds();
Output:
45
JavaScript code to demonstrate an example of Date.setSeconds() method
<html>
<head><title>JavaScipt Example</title></head>
<body>
<script>
var dt = new Date();
//getting current Seconds
var Second = dt.getSeconds();
//printing
document.write("Second: " + Second + "<br>");
//setting Second to 45
dt.setSeconds(45);
//getting & printing Seconds again
Second = dt.getSeconds();
document.write("Second: " + Second + "<br>");
//setting invalid Seconds
//setting Second to 61
dt.setSeconds(61);
//getting & printing Seconds again
Second = dt.getSeconds();
document.write("Second: " + Second + "<br>");
</script>
</body>
</html>
Output
Second: 41
Second: 45
Second: 1
Reference: JavaScript setSeconds() Method
JavaScript Date Object Methods »