Last active
August 29, 2015 13:57
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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