Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

JavaScript – Time Conversion – Millisecond (Execution/Run Time) To Human Readable

Posted on December 26, 2018December 26, 2018 By David Kittell

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>
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code JavaScript

Post navigation

Previous post
Next post

Related Posts

NetSuite – SuiteScript 2.0 – Useful Functions

Posted on August 31, 2018

These first function snippets are from https://gist.github.com/W3BGUY All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears…

Read More

Ektron Find Replace In Sitemap

Posted on October 23, 2013October 26, 2015

This script will search and replace sitemap links/titles/etc Source: http://www.skonet.com/Articles_Archive/Helpful_Sql_Scripts_for_Ektron_CMS_400_Net.aspx Originally Posted on October 23, 2013Last Updated on October 26, 2015 All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you…

Read More

Web Crawler – C#

Posted on August 12, 2013October 26, 2015

Originally Posted on August 12, 2013Last Updated on October 26, 2015 All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes
 

Loading Comments...
 

You must be logged in to post a comment.