Skip to content

Instantly share code, notes, and snippets.

@crestinglight
Created February 21, 2017 03:52
Show Gist options
  • Select an option

  • Save crestinglight/27f61f84bc81f4957ff680e65df0e2d8 to your computer and use it in GitHub Desktop.

Select an option

Save crestinglight/27f61f84bc81f4957ff680e65df0e2d8 to your computer and use it in GitHub Desktop.
function convertToReadableTime(millis){
var timeArray = [];
var hundredths = findHundredths(millis);
var x = Math.floor(millis / 1000);
var seconds = Math.floor(x % 60);
x /= 60;
var minutes = Math.floor(x % 60);
x /= 60;
var hours = Math.floor(x % 24);
x /= 24;
var days = Math.floor(x);
timeArray.push(hundredths, seconds, minutes, hours, days);
var timeString = timeFormat(timeArray, 2);
return timeString;
}
function findHundredths(millis){
var secondsDecimal = millis / 1000;
var x = String(secondsDecimal);
var splitHundredths = x.split(".");
var bajillionHundredths = splitHundredths[1];
var newHundredths = bajillionHundredths.charAt(0) + bajillionHundredths.charAt(1);
return newHundredths;
}
function timeFormat(array, targetLength){
var formattedArray = [];
for(var i = 0; i < array.length; i++){
var output = array[i] + '';
while (output.length < targetLength) {
output = '0' + output;
}
formattedArray.push(output);
}
var finalTimeString = stringItTogether(formattedArray);
return finalTimeString;
}
function stringItTogether(arrayOfTimes){
var stringy = arrayOfTimes[4] + ":" + arrayOfTimes[3] + ":" + arrayOfTimes[2] + ":" + arrayOfTimes[1] + "." + arrayOfTimes[0]
return stringy;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment