Currently, my script is working as expected, but I want to show milliseconds in 3 decimal places less than 100
Here is my function:
var updateTime = function () {
var tempTime = elapsedTime;
var milliseconds = tempTime % 1000;
tempTime = Math.floor(tempTime / 1000);
var seconds = tempTime % 60;
tempTime = Math.floor(tempTime / 60);
var minutes = tempTime % 60;
tempTime = Math.floor(tempTime / 60);
var hours = tempTime % 60;
time = (hours) ":" (minutes) ":" (seconds) "." milliseconds;
console.log(milliseconds)
document.getElementById('time').innerText = time
};
console.log output:
0
0
2
4
5
10
14
17
21
25
30
33
38
41
46
...
81
85
89
93
97
102
105
109
113
117
122
125
How can I make the output be like this? =>
000
000
002
004
005
010
014
017
021
025
046
...
081
085
089
093
097
102
105
I tried using milliseconds.toFixed(3)
but nothing happened! Could you please help me?
Thank you.
CodePudding user response:
Try this
const fmtNum = (num, pad) => String(num).padStart(pad, "0");
time = `${fmtNum(hours, 2)}:${fmtNum(minutes, 2)}:${fmtNum(seconds, 2)}.${fmtNum(milliseconds, 3)}`;