日期setSeconds()方法以及JavaScript中的示例

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

参考: JavaScript setSeconds()方法

翻译自: https://www.includehelp.com/code-snippets/date-setSeconds-method-with-example-in-javascript.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值