I am getting hours exceed two charecter, when I am trying to convert seconds to hh:mm:ss format.
var seconds = 4287050531;
var getTime = formatTime(seconds);
console.log("Time Is :"+getTime);// 1190847:22:11
function formatTime(seconds) {
return [pad(Math.floor(seconds/3600)),
pad(Math.floor(seconds/60)%60),
pad(seconds%60),
].join(":");
}
function pad(num) {
if(num < 10) {
return "0" + num;
} else {
return "" + num;
}
}
Can you please try with below function:
function convert(seconds) {
seconds = Number(seconds);
var hours = Math.floor(seconds / 3600);
var minutes = Math.floor(seconds % 3600 / 60);
var seconds = Math.floor(seconds % 3600 % 60);
return ((hours > 0 ? hours + ":" + (minutes < 10 ? "0" : "") : "") + minutes + ":" + (seconds < 10 ? "0" : "") +`enter code here` seconds);
}
Why don't you use moment-duration-format module
npm install moment-duration-format
var moment = require("moment-duration-format");
moment.duration(seconds, "seconds").format("h:m:s");
Why take all the trouble?
Try this:
(function () { function checkTime(i) { return (i < 10) ? "0" + i : i; } function startTime() { var today = new Date(), h = checkTime(today.getHours()), m = checkTime(today.getMinutes()), s = checkTime(today.getSeconds()); return h + ":" + m + ":" + s; } setInterval(function () { document.getElementById('yourTime').innerHTML = startTime(); }, 1000); })();
<div id="yourTime">
Try to use datejs
Using this can can easily convert it
(new Date).clearTime()
.addSeconds(4287050531)
.toString('H:mm:ss');
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.