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


JavaScript Date setMilliseconds()用法及代碼示例


下麵是Date setMilliseconds()方法的示例。

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

date.setMilliseconds()方法用於將毫秒數設置為使用date()構造函數創建的日期對象。

用法:

dateObj.setMilliseconds(milliseconds_Value);

參數:此方法接受如上所述和以下描述的單個參數:

  • milliseconds_Value:此參數保留用於在date()構造函數中設置的毫秒值。
  • 返回值:它返回由setMilliseconds()方法設置的新的即更新的毫秒數。



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

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

    程序1:如果在Date()構造函數中沒有給出任何毫秒數,則setMilliseconds()方法仍然設置新的毫秒數作為其參數。

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

    Output:

51

程序2:如果在Date()構造函數中未提供任何參數作為參數,則仍將setMilliseconds()方法設置為毫秒,但月,年,日期等變為當前方法。這裏42是新毫秒數,4是當前月份,即4月,1是當前日期,2018是當前年份。

<script> 
   // Here nothing has been assigned 
   // while creating Date object 
   var dateobj = new Date(); 
  
   // new millisecond of 42 is being set in  
   // above Date Object with the help of  
   // setMilliseconds() method 
   dateobj.setMilliseconds(42); 
  
   // Milliseconds from above Date Object is 
   // being extracted using getMilliseconds() 
   var B = dateobj.getMilliseconds(); 
  
   // 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 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
4
1
2018

範例3:如果將毫秒值1006設置為setMilliseconds()方法的參數,則會將6設置為毫秒,因為毫秒範圍是0到999,因此1006-1000=6,此處減去1000,因為0到999為1000。
這裏的6是新的毫秒數,第二個秒數從32變為33,因為毫秒數範圍是從0到999,即總計1000,我們將新的毫秒數設置為1006,將第二個毫秒數從32增加到33,毫秒數變成了6。

<script> 
   // Here date has been assigned 
   // while creating Date object 
   var dateobj =  
   new Date('October 13, 1996 05:35:32:45'); 
  
   // new millisecond of 1006 is being set  
   // in above Date Object with the help of  
   // setMilliseconds() method 
   dateobj.setMilliseconds(1006); 
  
   // milliseconds from above Date Object is 
   // being extracted using getMilliseconds() 
   var B = dateobj.getMilliseconds(); 
  
   // Second from above Date Object is 
   // being extracted using getSeconds() 
   var C = dateobj.getSeconds(); 
  
   // Printing new Milliseconds 
   document.write(B + "<br />"); 
  
   // Printing second 
   document.write(C); 
</script>

輸出:

6
33

支持的瀏覽器:下麵列出了JavaScript Date setMilliseconds()方法支持的瀏覽器:

  • 穀歌瀏覽器
  • IE瀏覽器
  • 火狐瀏覽器
  • Opera
  • Safari




相關用法


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