我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)
我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。
那么有一个小片段,这是在jQuery或只是原始JavaScript?
我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)
我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。
那么有一个小片段,这是在jQuery或只是原始JavaScript?
当前回答
这里有一个相当简单的解决方案,四舍五入到最近的秒!
var returnElapsedTime =函数(epoch) { //我们假设epoch以秒为单位 Var小时= epoch / 3600, 分钟=(小时% 1)* 60, 秒=(分钟% 1)* 60; 返回Math.floor(小时)+ ":" + Math.floor(分钟)+ ":" + Math.round(秒); }
其他回答
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());
你可以使用moment .js的moment-duration-format插件:
Var秒= 3820; Var duration =时刻。持续时间(秒,“秒”); Var format = duration.format("hh:mm:ss"); console.log(格式化);/ / 01:03:40 <!——Moment.js库——> .js库 < script src = " https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js " > < /脚本> <!——moment-duration-format插件——> < script src = " https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js " > < /脚本>
看看这把小提琴
毫秒到持续时间,简单的方法是:
// To have leading zero digits in strings.
function pad(num, size) {
var s = num + "";
while (s.length < size) s = "0" + s;
return s;
}
// ms to time/duration
msToDuration = function(ms){
var seconds = ms / 1000;
var hh = Math.floor(seconds / 3600),
mm = Math.floor(seconds / 60) % 60,
ss = Math.floor(seconds) % 60,
mss = ms % 1000;
return pad(hh,2)+':'+pad(mm,2)+':'+pad(ss,2)+'.'+pad(mss,3);
}
它将327577转换为00:05:27.577。
更新
另一种不同场景的方法:
toHHMMSS = function (n) {
var sep = ':',
n = parseFloat(n),
sss = parseInt((n % 1)*1000),
hh = parseInt(n / 3600);
n %= 3600;
var mm = parseInt(n / 60),
ss = parseInt(n % 60);
return pad(hh,2)+sep+pad(mm,2)+sep+pad(ss,2)+'.'+pad(sss,3);
function pad(num, size) {
var str = num + "";
while (str.length < size) str = "0" + str;
return str;
}
}
toHHMMSS(6315.077) // Return 01:45:15.077
这是另一个版本,也处理天数:
function FormatSecondsAsDurationString( seconds )
{
var s = "";
var days = Math.floor( ( seconds / 3600 ) / 24 );
if ( days >= 1 )
{
s += days.toString() + " day" + ( ( days == 1 ) ? "" : "s" ) + " + ";
seconds -= days * 24 * 3600;
}
var hours = Math.floor( seconds / 3600 );
s += GetPaddedIntString( hours.toString(), 2 ) + ":";
seconds -= hours * 3600;
var minutes = Math.floor( seconds / 60 );
s += GetPaddedIntString( minutes.toString(), 2 ) + ":";
seconds -= minutes * 60;
s += GetPaddedIntString( Math.floor( seconds ).toString(), 2 );
return s;
}
function GetPaddedIntString( n, numDigits )
{
var nPadded = n;
for ( ; nPadded.length < numDigits ; )
{
nPadded = "0" + nPadded;
}
return nPadded;
}
function toHHMMSS(seconds) {
var h, m, s, result='';
// HOURs
h = Math.floor(seconds/3600);
seconds -= h*3600;
if(h){
result = h<10 ? '0'+h+':' : h+':';
}
// MINUTEs
m = Math.floor(seconds/60);
seconds -= m*60;
result += m<10 ? '0'+m+':' : m+':';
// SECONDs
s=seconds%60;
result += s<10 ? '0'+s : s;
return result;
}
例子
toHHMMSS(111); "01:51" toHHMMSS(4444); "01:14:04" toHHMMSS(33); "00:33"