Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Last active December 10, 2015 08:28
Show Gist options
  • Save kristopherjohnson/4408212 to your computer and use it in GitHub Desktop.
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")
// 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