下面是一个例子 日期设置单位毫秒() 方法
- 例子:
Javascript
<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
这个 日期setutcmillesons() 方法用于根据通用时间将毫秒设置为使用date()构造函数创建的日期对象。
语法:
DateObj.setUTCMilliseconds(milliseconds_Value);
参数: 此方法接受一个参数,如上所述,如下所述:
- 单位值: 此参数保存用于在Date()构造函数中设置的毫秒值。
返回值: 它返回由setutcmillesons()方法设置的新的即更新的毫秒。
注: 这个 DateObj 是使用Date()构造函数创建的有效日期对象,我们希望在其中根据世界时设置毫秒。毫秒的值介于0到999之间。
以上方法的更多代码如下:
项目1: 如果在Date()构造函数中,我们不提供 毫秒 在创建日期对象时,仍然可以使用setutcmillisons()方法设置新的毫秒,该毫秒在创建的日期对象中作为参数给出。
Javascript
<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()构造函数中没有给出任何参数,那么SetUtcMillSeconds()方法将能够设置毫秒,但根据世界时,月份、年份、日期等仍然是当前值。 这里42是新的毫秒,3是当前月份,即4月,1是当前日期,2018年是世界时的当前年份。
Javascript
<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> |
输出:
42312018
方案3: 如果将毫秒1006的值作为setutcmillesons()方法的参数,它会将6设置为毫秒,因为毫秒范围为0到999,因此 ,这里减去1000,因为0到999是1000。
Javascript
<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> |
输出:
633
支持的浏览器: 支持的浏览器 JavaScript Date setutcmillesons()方法 以下列出了:
- 谷歌Chrome 1及以上版本
- 边缘12及以上
- Firefox 1及以上版本
- Internet Explorer 4及以上版本
- 歌剧4及以上
- Safari 1及以上