Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

79
Views
Javascript milliseconds 3 decimal places

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try this

const fmtNum = (num, pad) => String(num).padStart(pad, "0");

time = `${fmtNum(hours, 2)}:${fmtNum(minutes, 2)}:${fmtNum(seconds, 2)}.${fmtNum(milliseconds, 3)}`;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!