Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


converting milliseconds to hours, minutes, seconds in javascript

Posted on 2009-02-10
Medium Priority
Last Modified: 2012-05-06
Could somebody give me some javascript code. Lets say I have a var called varmilliseconds ="7395000". That variable is the number of milliseconds. I want a formula to convert this to the format time 02:03:15. That is 2 hours, 3 minutes and 15 seconds (Which is 7395000 milliseconds).

If somebody could give me the code that would be great. Thanks.
Question by:RupertA
LVL 15

Accepted Solution

fsze88 earned 2000 total points
ID: 23601779

<script type="text/javascript">
var d = new Date();
document.write(d.getHours() + ":" +d.getMinutes()+":" +d.getSeconds() +"<br/>");

Open in new window

LVL 15

Expert Comment

ID: 23601810
LVL 15

Expert Comment

ID: 23601841
try this (untested)
var milliseconds = parseInt("7395000");
var hours = Math.floor(milliseconds / 3600000);
var minutes = Math.floor((milliseconds - (hours * 3600000)) / 60000);
var seconds = parseInt((milliseconds - (hours * 3600000) - (minutes * 60000)) / 1000);

Open in new window

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 15

Expert Comment

ID: 23601876
yeah go with the date object, much cleaner.

Expert Comment

ID: 23602000
Or if you want the leading zeros, you have can add them explicitly like this, say:
<script language=javascript>
function cvt()
var varmilliseconds = document.getElementById('ms').value;    //get millisecs as string
var s= cvtfrom_ms(varmilliseconds);
document.getElementById('res').innerHTML = s;
function cvtfrom_ms(s)
var d = new Date(parseInt(s));
return   to2digits(d.getHours()) + ":" + to2digits(d.getMinutes()) + ":" + to2digits(d.getSeconds());
function to2digits(n)
if (n > 10) return n;
return "0" + n;
Millisec: <input type=text id=ms onchange=cvt();> = <span id=res></span>

Open in new window


Author Comment

ID: 23602063
Yeah, I will go with fsze88's solution if you don't mind MMDeveloper. It is much cleaner, like you say and accurate.

I have been messing around all today with doing calculations and getting totally confused and I hadn't realised that there are built in functions that will do all the work for you. Such is my level of knowledge.

Thanks fsze88 for enlightening me.
LVL 15

Expert Comment

ID: 23602388
no not at all, as soon as I posted my code it hit me (you know I bet there's some date methods that do this already) but it was too late, someone else had already posted them, no worries.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question