Skip to content

Instantly share code, notes, and snippets.

@0xB10C
Created August 13, 2017 21:38
Show Gist options
  • Save 0xB10C/2ea433a5dd58c1142328e5c1237ff61d to your computer and use it in GitHub Desktop.
Save 0xB10C/2ea433a5dd58c1142328e5c1237ff61d to your computer and use it in GitHub Desktop.
A function that converts an integer (seconds) to a DHHMMSS string.
String.prototype.toDHHMMSS = function () {
var sec_num = parseInt(this, 10);
var days = Math.floor(sec_num / 86400)
var hours = Math.floor((sec_num - (days * 86400)) / 3600);
var minutes = Math.floor(((sec_num - (days * 86400)) - (hours * 3600)) / 60);
var seconds = sec_num - (days * 86400) - (hours * 3600) - (minutes * 60);
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return days + "d " + hours+'h '+minutes+'m '+seconds+"s";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment