JavaScript Date setSeconds()方法 (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).
setSeconds()方法是Date类的方法,用于将秒数设置为具有有效第二个值(介于00到59之间)的Date对象。
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.
注意:大于59的秒数将从开始的秒数(00)开始被截断,例如,我们将61设置为第二个值,它将被截断为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
JavaScript代码演示Date.setSeconds()方法的示例
<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
翻译自: https://www.includehelp.com/code-snippets/date-setSeconds-method-with-example-in-javascript.aspx