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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。