下面是一个例子 日期设置毫秒() 方法
- 例子:
<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
这个 日期setms() 方法用于将毫秒设置为使用date()构造函数创建的日期对象。
语法:
dateObj.setMilliseconds(milliseconds_Value);
参数: 此方法接受一个参数,如上所述,如下所述:
- 单位值: 此参数保存用于在date()构造函数中设置的毫秒值。
返回值: 它返回新的,即更新的毫秒,该毫秒由setMillimes()方法设置。
注: 这个 DateObj 是使用Date()构造函数创建的有效日期对象,我们希望在其中设置毫秒。毫秒的值在0到999之间。
以上方法的更多代码如下:
项目1: 如果在Date()构造函数中,我们没有给出任何毫秒,那么setmillizes()方法仍然会设置新的毫秒,该毫秒是作为参数给出的。
<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>
输出:
51
项目2: 如果Date()构造函数中没有给出任何参数,则setMillimes()方法设置毫秒,但月、年、日期等变为当前值。这里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的值作为setmillizes()方法的参数,它会将6设置为毫秒,因为毫秒范围是从0到999,因此
,这里减去1000,因为0到999是1000。 这里6是新的毫秒,从32变为33,因为毫秒的范围是从0到999,也就是说,总计1000,我们将新的毫秒设置为1006,它从32增加1到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 setmillizes()方法 以下列出了:
- 谷歌Chrome 1及以上版本
- 边缘12及以上
- Firefox 1及以上版本
- Internet Explorer 4及以上版本
- 歌剧4及以上
- Safari 1及以上