当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JavaScript Date setUTCMilliseconds()用法及代码示例


下面是Date 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() method 
       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

date.setUTCMilliseconds()方法用于根据通用时间将毫秒设置为使用Date()构造函数创建的日期对象。

用法:

DateObj.setUTCMilliseconds(milliseconds_Value);

参数:该方法接受上述和以下描述的单个参数:

  • milliseconds_Value:此参数保留用于在Date()构造函数中设置的毫秒值。

返回值:它返回由setUTCMilliseconds()方法设置的新的即更新的毫秒数。



注意:DateObj是使用Date()构造函数创建的有效Date对象,其中我们要根据通用时间设置毫秒。毫秒值是从0到999。

上述方法的更多代码如下:
程序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() method 
   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()方法仍然可以设置毫秒,但是根据通用时间,月,年,日期等仍为当前时间。
根据通用时间,此处42是新毫秒,3是当前月份,即4月,1是当前日期,2018是当前年份。

<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() method 
   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

程序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() method 
   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
  • Safari




相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 JavaScript Date setUTCMilliseconds() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。