Last active
December 10, 2015 08:28
-
-
Save kristopherjohnson/4408212 to your computer and use it in GitHub Desktop.
Given a number of seconds, create a string of the form "Nd NN:NN:NN" (e.g., "5d 17:13:46")
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
// Given numeric value, convert to string with enough leading zeros | |
// to make it two characters long | |
var zeroPad2 = function (val) { | |
result = val.toString(); | |
while (result.length < 2) { | |
result = "0" + result; | |
} | |
return result; | |
}; | |
// Given number of seconds, return string of form "Nd NN:NN:NN" | |
var stringFromSeconds = function (sec) { | |
try { | |
var days = Math.floor(sec / 86400); | |
sec = sec - days * 86400; | |
var hours = Math.floor(sec / 3600); | |
sec = sec - hours * 3600; | |
var minutes = Math.floor(sec / 60); | |
sec = Math.floor(sec - minutes * 60); | |
return days.toString() + "d " + zeroPad2(hours) + ":" + zeroPad2(minutes) + ":" + zeroPad2(sec); | |
} | |
catch (err) { | |
return ""; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment