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


Javascript date.setSeconds()用法及代碼示例


date.setSeconds()是JavaScript中的內置函數,用於將秒設置為使用Date()構造函數創建的Date對象。

用法:

DateObj.setSeconds(seconds_Value)

DateObj是使用Date()構造函數創建的有效Date對象,我們要在其中構造第二個對象。秒的值是從0到59。


參數:這裏的參數seconds_Value是秒的值,用於在Date()構造函數中設置。

返回值:它返回由setSeconds()函數設置的具有更新秒數的新日期。

下麵的程序演示了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() function 
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

錯誤和異常

  • 示例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() function 
    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()函數設置為第二,但是將月份,年份,日期等作為當前函數。
    <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() function 
    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

    這裏42是新的秒數,3是當前月份,即4月,1是當前日期,2018是當前年份。

  • 示例3:如果將秒66的值作為setSeconds()函數的參數給出,則它將6設置為秒,因為第二範圍是從0到59的形式,並且(66%60 = 6)。
    <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

    此處6是新的秒,分鍾從35變為36,因為第二範圍是從0到59。因此,當將66作為參數傳遞給setSeconds()函數時,它將把相應Date對象中的秒設置為(66%60 = 6 ),並將相應的Date對象中的分鍾數增加(66/60 = 1)。

支持的瀏覽器:JavaScript date.setSeconds()函數支持的瀏覽器如下:

  • 穀歌瀏覽器
  • IE瀏覽器
  • 火狐瀏覽器
  • Opera
  • 蘋果瀏覽器


相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 JavaScript | date.setSeconds() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。