Skip to content

Instantly share code, notes, and snippets.

@shubhamjain
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shubhamjain/9442617 to your computer and use it in GitHub Desktop.
Save shubhamjain/9442617 to your computer and use it in GitHub Desktop.
This function takes milliseconds as input and converts it into human readable form. Useful in showing time elapsed or ETA.
/**
* This function takes milliseconds as input and converts it into "D H M S ms" format.
*
* @author Shubham Jain <shubham.jain.1@gmail.com>
* @license MIT License
*/
function timeFormat( miliseconds )
{
formatArr = {
"d": 24 * 60 * 60 * 1000,
"h": 60 * 60 * 1000,
"m": 60 * 1000,
"s": 1000,
"ms": 1
}
var timeStr = "";
for ( index in formatArr )
{
if( Math.floor(timeUnit = miliseconds / formatArr[ index ]) )
{
timeStr += Math.floor(timeUnit) + index + " ";
miliseconds = miliseconds % formatArr[ index ];
}
};
return timeStr.trim();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment