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


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


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

用法:

DateObj.setUTCMilliseconds(milliseconds_Value);

DateObj是使用Date()構造函數創建的有效Date對象,在該構造函數中,我們要根據通用時間設置毫秒。毫秒值是從0到999。


參數:這裏的參數是milliseconds_Value是在Date()構造函數中設置的毫秒值。

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

以下示例程序旨在說明setUTCMilliseconds()函數:

<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:77 GMT-3:00'); 
  
// new millisecond of 52 is being set in above Date 
// Object with the help of setUTCMilliseconds() function 
dateobj.setUTCMilliseconds(52); 
  
// new millisecond from above Date Object is 
// being extracted using getUTCMilliseconds() 
var B = dateobj.getUTCMilliseconds(); 
  
// Printing new millisecond 
document.write(B); 
</script>

輸出:

52

錯誤和異常

  • 示例1:如果在Date()構造函數中創建日期對象時不提供毫秒數,則setUTCMilliseconds()函數仍然可以設置新的毫秒數,該毫秒數作為在創建的Date對象中的參數給出。
    <script> 
    // Here millisecond has not been assigned according to 
    // universal time while creating Date object 
    var dateobj = new Date('October 13, 1996 GMT-3:00'); 
      
    // new millisecond of 51 is being set in above Date 
    // Object with the help of setUTCMilliseconds() function 
    dateobj.setUTCMilliseconds(51); 
      
    // new millisecond from above Date Object is 
    // being extracted using getUTCMilliseconds() 
    var B = dateobj.getUTCMilliseconds(); 
      
    // Printing new millisecond 
    document.write(B); 
    </script>

    輸出:

    51
  • 示例2:如果在Date()構造函數中未提供任何參數作為參數,則setUTCMilliseconds()函數仍將能夠設置毫秒,但根據通用時間,月,年,日期等仍為當前時間。
    <script> 
    // Here nothing has been assigned according to 
    // universal time while creating Date object 
    var dateobj = new Date(); 
      
    // new millisecond of 42 is being set in above Date 
    // Object with the help of setUTCMilliseconds() function 
    dateobj.setUTCMilliseconds(42); 
      
    // milliseconds from above Date Object is 
    // being extracted using getUTCMilliseconds() 
    var B = dateobj.getUTCMilliseconds(); 
      
    // month from above Date Object is 
    // being extracted using getUTCMonth() 
    var C = dateobj.getUTCMonth(); 
      
    // date from above Date Object is 
    // being extracted using getDate() 
    var D = dateobj.getUTCDate(); 
      
    // year from above Date Object is 
    // being extracted using getUTCFullYear() 
    var E = dateobj.getUTCFullYear(); 
      
    // Printing new milliseconds 
    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:如果將毫秒值1006設置為setUTCMilliseconds()函數的參數,則會將6設置為毫秒,因為毫秒範圍是從0到999,因此1006-1000=6,此處減去1000,因為0到999為1000。
    <script> 
    // Here date has been assigned according to 
    // universal time while creating Date object 
    var dateobj = new Date('October 13, 1996 05:35:32:45 GMT-3:00'); 
      
    // new millisecond of 1006 is being set in above Date 
    // Object with the help of setUTCMilliseconds() function 
    dateobj.setUTCMilliseconds(1006); 
      
    // milliseconds from above Date Object is 
    // being extracted using getUTCMilliseconds() 
    var B = dateobj.getUTCMilliseconds(); 
      
    // Second from above Date Object is 
    // being extracted using getUTCSeconds() 
    var C = dateobj.getUTCSeconds(); 
      
    // Printing new Milliseconds 
    document.write(B + "<br>"); 
      
    // Printing second 
    document.write(C); 
    </script>

    輸出:

    6
    33

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

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


相關用法


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