当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JavaScript Date setSeconds()用法及代码示例


下面是Date setSeconds()方法的示例。

  • 例:
    <script> 
       // Here a date has been assigned 
       // while creating Date object 
       var dateobj = 
       new Date('October 13, 1996 05:35:32'); 
      
       // New second of 52 is being set in above Date 
       // Object with the help of setSeconds() method 
       dateobj.setSeconds(52); 
      
       // New second from above Date Object is 
       // being extracted using getSeconds() 
       var B = dateobj.getSeconds(); 
      
       // Printing new Second 
       document.write(B); 
    </script>
  • 输出:
    52

date.setSeconds()方法用于将秒设置为使用Date()构造函数创建的Date对象。

用法:

DateObj.setSeconds(seconds_Value)

参数:此方法接受上述和以下描述的单个参数:

  • seconds_Value:此参数保留用于在Date()构造函数中设置的秒值。

返回值:它返回具有更新的秒的新日期,该日期由setSeconds()方法设置。



注意:DateObj是使用Date()构造函数创建的有效Date对象,我们要在其中构造第二个对象。秒的值是从0到59。

上述方法的更多代码如下:

程序1:如果在Date()构造函数中没有给出任何第二个,则setSeconds()方法仍然设置了新的第二个作为其参数。

<script> 
   // Here second has not been assigned 
   // while creating Date object 
   var dateobj = new Date('October 13, 1996'); 
  
   // New second of 51 is being set in above Date 
   // Object with the help of setSeconds() method 
   dateobj.setSeconds(51); 
  
   // New second from above Date Object is 
   // being extracted using getSeconds() 
   var B = dateobj.getSeconds(); 
  
   // Printing new Second 
   document.write(B); 
</script>

输出:

51

范例2:如果在Date()构造函数中未提供任何参数作为参数,则仍将setSeconds()方法设置为第二,但是将月,年,日期等设置为当前方法。这里42是新的秒,3是当前月,即4月,1是当前日期,2018是当前年。

<script> 
   // Here nothing has been assigned 
   // while creating Date object 
   var dateobj = new Date(); 
  
   // New second of 42 is being set in above Date 
   // Object with the help of setSeconds() method 
   dateobj.setSeconds(42); 
  
   // Seconds from above Date Object is 
   // being extracted using getSeconds() 
   var B = dateobj.getSeconds(); 
  
   // Month from above Date Object is 
   // being extracted using getMonth() 
   var C = dateobj.getMonth(); 
  
   // Date from above Date Object is 
   // being extracted using getDate() 
   var D = dateobj.getDate(); 
  
   // Year from above Date Object is 
   // being extracted using getFullYear() 
   var E = dateobj.getFullYear(); 
  
   // Printing new seconds 
   document.write(B + "<br />"); 
     
   // Printing current month 
   document.write(C + "<br />"); 
  
   // Printing current date 
   document.write(D + "<br />"); 
  
   // Printing current year 
   document.write(E); 
</script>

输出:

42
3
1
2018

范例3:如果将秒66的值作为setSeconds()函数的参数给出,则它将6设置为秒,因为秒的范围是0到59和(66%60 = 6)。
此处6是新的秒,分钟数从35变为36,因为第二范围是0到59。因此当将66作为参数传递给setSeconds()函数时,它将在相应的Date对象中将秒设置为(66%60 = 6),并将相应的Date对象中的分钟数增加(66/60 = 1)。

<script> 
   // Here date has been assigned 
   // while creating Date object 
   var dateobj =  
   new Date('October 13, 1996 05:35:32'); 
  
   // New second of 66 is being set in above Date 
   // Object with the help of setSeconds() function 
   dateobj.setSeconds(66); 
  
   // Seconds from above Date Object is 
   // being extracted using getSeconds() 
   var B = dateobj.getSeconds(); 
  
   // Minute from above Date Object is 
   // being extracted using getMinutes() 
   var C = dateobj.getMinutes(); 
  
   // Printing new seconds 
   document.write(B + "<br />"); 
  
   // Printing minutes 
   document.write(C); 
</script>

输出:

6
36

支持的浏览器:下面列出了JavaScript Date setSeconds()方法支持的浏览器:

  • 谷歌浏览器
  • IE浏览器
  • 火狐浏览器
  • Opera
  • Safari




相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 JavaScript Date setSeconds() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。