I want to convert time to Days, Hours, Minutes and seconds, i've found this:
String.prototype.toHHMMSS = function () {
var sec_num = parseInt(this, 10); // don't forget the second param
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
var seconds = sec_num - (hours * 3600) - (minutes * 60);
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return days+':'+hours+':'+minutes+':'+seconds;
}
But i don't know how to display 1 day when hours is superior then 24 hours
Thanks !
You can do that by dividing seconds by sec_num / (3600 * 24) kind of same as you did for hours just multiply it by 24, below is the updated working code.
String.prototype.toHHMMSS = function () {
var sec_num = parseInt(this, 10); // don't forget the second param
var days = Math.floor(sec_num / (3600 * 24));
var hours = Math.floor((sec_num - (days * (3600 * 24))) / 3600);
var minutes = Math.floor((sec_num - (days * (3600 * 24)) - (hours * 3600)) / 60);
var seconds = sec_num - (days * (3600 * 24)) - (hours * 3600) - (minutes * 60);
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return days+':'+hours+':'+minutes+':'+seconds;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.