我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)
我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。
那么有一个小片段,这是在jQuery或只是原始JavaScript?
我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)
我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。
那么有一个小片段,这是在jQuery或只是原始JavaScript?
当前回答
secToHHMM(number: number) {
debugger;
let hours = Math.floor(number / 3600);
let minutes = Math.floor((number - (hours * 3600)) / 60);
let seconds = number - (hours * 3600) - (minutes * 60);
let H, M, S;
if (hours < 10) H = ("0" + hours);
if (minutes < 10) M = ("0" + minutes);
if (seconds < 10) S = ("0" + seconds);
return (H || hours) + ':' + (M || minutes) + ':' + (S || seconds);
}
其他回答
我不喜欢在JavaScript中为标准数据类型添加属性,所以我建议这样做:
/**
* Format a duration in seconds to a human readable format using the notion
* "h+:mm:ss", e.g. "4:40:78". Negative durations are preceeded by "-".
*
* @param t Duration in seconds
* @return The formatted duration string
*/
var readableDuration = (function() {
// Each unit is an object with a suffix s and divisor d
var units = [
{s: '', d: 1}, // Seconds
{s: ':', d: 60}, // Minutes
{s: ':', d: 60}, // Hours
];
// Closure function
return function(t) {
t = parseInt(t); // In order to use modulus
var trunc, n = Math.abs(t), i, out = []; // out: list of strings to concat
for (i = 0; i < units.length; i++) {
n = Math.floor(n / units[i].d); // Total number of this unit
// Truncate e.g. 26h to 2h using modulus with next unit divisor
if (i+1 < units.length) // Tweak substr with two digits
trunc = ('00'+ n % units[i+1].d).substr(-2, 2); // …if not final unit
else
trunc = n;
out.unshift(''+ trunc + units[i].s); // Output
}
(t < 0) ? out.unshift('-') : null; // Handle negative durations
return out.join('');
};
})();
用法:
var str = readableDuration(3808); // "1:03:28"
我还创建了一个更通用的版本。主要的区别是它接受毫秒(这是JS中的标准时间单位),而输出格式使用空格。
在谷歌上搜索的结果是这样的:
function secondsToTime(secs)
{
secs = Math.round(secs);
var hours = Math.floor(secs / (60 * 60));
var divisor_for_minutes = secs % (60 * 60);
var minutes = Math.floor(divisor_for_minutes / 60);
var divisor_for_seconds = divisor_for_minutes % 60;
var seconds = Math.ceil(divisor_for_seconds);
var obj = {
"h": hours,
"m": minutes,
"s": seconds
};
return obj;
}
我最喜欢Webjins的答案,所以我扩展了它以显示d后缀的日子,使显示有条件,并包括一个s后缀的普通秒:
function sec2str(t){
var d = Math.floor(t/86400),
h = ('0'+Math.floor(t/3600) % 24).slice(-2),
m = ('0'+Math.floor(t/60)%60).slice(-2),
s = ('0' + t % 60).slice(-2);
return (d>0?d+'d ':'')+(h>0?h+':':'')+(m>0?m+':':'')+(t>60?s:s+'s');
}
返回"3d 16:32:12"或"16:32:12"或"32:12"或"12s"
你可以在没有任何外部JS库的帮助下使用JS Date方法做到这一点,如下所示:
var date = new date (0); date.setSeconds (45);//指定SECONDS的值 var timeString = date.toISOString()。substring(11、19); console.log (timeString)
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 hours+':'+minutes+':'+seconds;
}
你现在可以这样使用它:
alert("5678".toHHMMSS());
工作代码片段:
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 hours + ':' + minutes + ':' + seconds; } console.log("5678".toHHMMSS());