我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)

我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。

那么有一个小片段,这是在jQuery或只是原始JavaScript?


当前回答

我最喜欢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"

其他回答

如果你是在处理视频长度,这个版本的公认答案会让它看起来更漂亮:

1:37:40(1小时/ 37分钟/ 40秒)

1:00(1分钟)

2:20(2分20秒)

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);

  var hourSeparator = ':';
  var minuteSeparator = ':';

  if(hours == 0){hours = '';hourSeparator = '';}
  if (minutes < 10 && hours != 0) {minutes = "0"+minutes;}
  if (seconds < 10) {seconds = "0"+seconds;}
  var time = hours+hourSeparator+minutes+minuteSeparator+seconds;
  return time;
}

toHHMMSS的非原型版本:

    function toHHMMSS(seconds) {
        var sec_num = parseInt(seconds);
        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;}
        var time    = hours+':'+minutes+':'+seconds;
        return time;
    }   

下面是一个使用Date.prototype.toLocaleTimeString()的例子。我选择GB作为语言,因为美国在最初的小时显示的是24而不是00。此外,我选择Etc/UTC作为时区,因为UTC在tz数据库时区列表中是它的别名。

const formatTime = (seconds) => 新日期(秒* 1000)。toLocaleTimeString(“en”{ 时区:“等/ UTC ', hour12:假的, 小时:“便是”, 分钟:“便是”, 第二:“便是” }); console.log (formatTime (75));/ / 00:01:15 .as-console-wrapper {top: 0;Max-height: 100%重要;}

下面是相同的示例,但是使用了Intl.DateTimeFormat。这个变体允许您实例化一个可重用的格式化器对象,这更具有性能。

const dateFormatter = new Intl。DateTimeFormat(“en”{ 时区:“等/ UTC ', hour12:假的, 小时:“便是”, 分钟:“便是”, 第二:“便是” }); const formatTime = (seconds) => dateFormatter。format(new Date(seconds * 1000)); console.log (formatTime (75));/ / 00:01:15 .as-console-wrapper {top: 0;Max-height: 100%重要;}

主题的变奏。处理个位数秒的方式有点不同

seconds2time(0)  ->  "0s" 
seconds2time(59) -> "59s" 
seconds2time(60) -> "1:00" 
seconds2time(1000) -> "16:40" 
seconds2time(4000) -> "1:06:40"

function seconds2time (seconds) {
    var hours   = Math.floor(seconds / 3600);
    var minutes = Math.floor((seconds - (hours * 3600)) / 60);
    var seconds = seconds - (hours * 3600) - (minutes * 60);
    var time = "";

    if (hours != 0) {
      time = hours+":";
    }
    if (minutes != 0 || time !== "") {
      minutes = (minutes < 10 && time !== "") ? "0"+minutes : String(minutes);
      time += minutes+":";
    }
    if (time === "") {
      time = seconds+"s";
    }
    else {
      time += (seconds < 10) ? "0"+seconds : String(seconds);
    }
    return time;
}
function secondsToTime(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);

    if(hours >= 12)
    {
     var m= 'pm' ;
     }
     else
     {
         var m='am'
     }
     if(hours-12 >0)
     {
            var hrs = hours-12;
     }
     else if(hours-12 <0)
     {
            var hrs = hours;
     }
    var obj = {
        "h": hrs,
        "m": minutes,
        "s": seconds,
        "a":m
    };


    return obj;
}
var d = new Date();
var n = d.getHours();
var hms = d.getHours()+':'+d.getMinutes()+':'+d.getSeconds();   // your input string
var a = hms.split(':'); // split it at the colons

// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); 




console.log(seconds);
console.log(secondsToTime(seconds))

https://jsfiddle.net/jithinksoft/9x6z4sdt/