當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。