JavaScript Date getMilliseconds()
示例
獲取毫秒數
const d = new Date();
let ms = d.getMilliseconds();
自己動手試一試 »
獲取特定日期的毫秒數
const d = new Date("July 21, 1983 01:15:00.250");
let ms = d.getMilliseconds();
自己動手試一試 »
更多示例見下文。
描述
getMilliseconds() 返回日期中的毫秒數(0 到 999)。
語法
Date.getMilliseconds()
引數
| 無 |
返回值
| 型別 | 描述 |
| 一個數字 | 日期的毫秒數(0 到 999)。 |
瀏覽器支援
getMilliseconds() 是 ECMAScript1 (ES1) 功能。
ES1 (JavaScript 1997) 在所有瀏覽器中都得到完全支援
| Chrome | Edge | Firefox | Safari | Opera | IE |
| 是 | 是 | 是 | 是 | 是 | 是 |
更多示例
新增零和冒號來顯示時間
function addZero(x, n) {
while (x.toString().length < n) {
x = "0" + x;
}
return x;
}
const d = new Date();
let h = addZero(d.getHours(), 2);
let m = addZero(d.getMinutes(), 2);
let s = addZero(d.getSeconds(), 2);
let ms = addZero(d.getMilliseconds(), 3);
let time = h + ":" + m + ":" + s + ":" + ms;
自己動手試一試 »