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


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

date.setUTCSeconds()是JavaScript中的內置函數,用於根據通用時間將秒設置為使用Date()構造函數創建的日期對象。

用法:

DateObj.setUTCSeconds(seconds_Value);

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


參數:在這裏,參數seconds_Value是我們想要在使用Date()構造函數創建的日期中設置的秒數。

返回值:它返回由setUTCSeconds()函數設置的新的即更新的秒。

下麵的程序演示了setUTCSeconds()函數:

<script> 
// Here a date has been assigned according to 
// universal time while creating Date object 
var dateobj = new Date('October 13, 1996 05:35:32 GMT-3:00'); 
  
// new second of 52 is being set in above Date 
// Object with the help of setUTCSeconds() function 
dateobj.setUTCSeconds(52); 
  
// new second from above Date Object is 
// being extracted using getUTCSeconds() 
var B = dateobj.getUTCSeconds(); 
  
// Printing new Second 
document.write(B); 
</script>

輸出:

52

錯誤和異常

  • 示例1:如果在Date()構造函數中創建日期對象時不給任何秒數,則setUTCSeconds()函數仍將能夠設置新的秒數,該秒數將根據創建的Date對象中的通用時間作為其參數給出。
    <script> 
    // Here second has not been assigned according to 
    // universal time while creating Date object 
    var dateobj = new Date('October 13, 1996 GMT-3:00'); 
      
    // new second of 51 is being set in above Date 
    // Object with the help of setUTCSeconds() function 
    dateobj.setUTCSeconds(51); 
      
    // new second from above Date Object is 
    // being extracted using getUTCSeconds() 
    var B = dateobj.getUTCSeconds(); 
      
    // Printing new Second 
    document.write(B); 
    </script>

    輸出:

    51
  • 示例2:如果在Date()構造函數中未提供任何參數,則setUTCSeconds()函數仍可以設置第二個,但月,年,日期等仍為當前值。
    <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 setUTCSeconds() function 
    dateobj.setUTCSeconds(42); 
      
    // Seconds from above Date Object is 
    // being extracted using getUTCSeconds() 
    var B = dateobj.getUTCSeconds(); 
      
    // month from above Date Object is 
    // being extracted using getUTCMonth() 
    var C = dateobj.getUTCMonth(); 
      
    // date from above Date Object is 
    // being extracted using getUTCDate() 
    var D = dateobj.getUTCDate(); 
      
    // year from above Date Object is 
    // being extracted using getUTCFullYear() 
    var E = dateobj.getUTCFullYear(); 
      
    // 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,此處減去60,因為0到59為60。
    <script> 
    // Here date has been assigned according to 
    // universal time while creating Date object 
    var dateobj = new Date('October 13, 1996 05:35:32 GMT-3:00'); 
      
    // new second of 66 is being set in above Date 
    // Object with the help of setUTCSeconds() function 
    dateobj.setUTCSeconds(66); 
      
    // seconds from above Date Object is 
    // being extracted using getUTCSeconds() 
    var B = dateobj.getUTCSeconds(); 
      
    // Minute from above Date Object is 
    // being extracted using getUTCMinutes() 
    var C = dateobj.getUTCMinutes(); 
      
    // Printing new seconds 
    document.write(B + "<br>"); 
      
    // Printing minutes 
    document.write(C); 
    </script>

    輸出:

    6
    36

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

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


相關用法


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