Sometimes when you are looking at execution time you want to make it easier to report the amount of time. The script below will help.
function msToTime(duration, OutputType) {
var milliseconds = parseInt((duration % 1000) / 100),
seconds = parseInt((duration / 1000) % 60),
minutes = parseInt((duration / (1000 * 60)) % 60),
hours = parseInt((duration / (1000 * 60 * 60)) % 24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
if (OutputType == 1) {
return hours + ":" + minutes + ":" + seconds + ":" + milliseconds;
} else {
return hours + " h " + minutes + " m " + seconds + " s " + milliseconds + ' ms';
}
}
function testFunction() {
document.write("<br>Output Type 1: " + msToTime(230495, 1));
document.write("<br>Output Type 2: " + msToTime(230495, 2));
}
Example Output:
Output Type 1: 00:03:50:4
Output Type 2: 00 h 03 m 50 s 4 ms
And a full example is below
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function msToTime(duration,OutputType) {
var milliseconds = parseInt((duration % 1000) / 100),
seconds = parseInt((duration / 1000) % 60),
minutes = parseInt((duration / (1000 * 60)) % 60),
hours = parseInt((duration / (1000 * 60 * 60)) % 24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
if (OutputType == 1)
{
return hours + ":" + minutes + ":" + seconds + ":" + milliseconds;
}
else
{
return hours + " h " + minutes + " m " + seconds + " s " + milliseconds + ' ms';
}
}
function testFunction()
{
document.write("<br>Output Type 1: " + msToTime(230495,1));
document.write("<br>Output Type 2: " + msToTime(230495,2));
}
</script>
<title>Time Conversion - Millisecond (Execution/Run Time) To Human Readable</title>
</head>
<body onload="testFunction()">
</body>
</html>