我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)
我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。
那么有一个小片段,这是在jQuery或只是原始JavaScript?
我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)
我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。
那么有一个小片段,这是在jQuery或只是原始JavaScript?
当前回答
以下是我的看法:
function formatTime(seconds) {
const h = Math.floor(seconds / 3600);
const m = Math.floor((seconds % 3600) / 60);
const s = Math.round(seconds % 60);
return [
h,
m > 9 ? m : (h ? '0' + m : m || '0'),
s > 9 ? s : '0' + s
].filter(Boolean).join(':');
}
预期结果:
const expect = require('expect');
expect(formatTime(0)).toEqual('0:00');
expect(formatTime(1)).toEqual('0:01');
expect(formatTime(599)).toEqual('9:59');
expect(formatTime(600)).toEqual('10:00');
expect(formatTime(3600)).toEqual('1:00:00');
expect(formatTime(360009)).toEqual('100:00:09');
expect(formatTime(0.2)).toEqual('0:00');
其他回答
使用moment.js库:
function humanizeDuration(input, units ) {
// units is a string with possible values of y, M, w, d, h, m, s, ms
var duration = moment().startOf('day').add(units, input),
format = "";
if(duration.hour() > 0){ format += "H [hours] "; }
if(duration.minute() > 0){ format += "m [minutes] "; }
format += " s [seconds]";
return duration.format(format);
}
这允许您指定任何持续时间,无论是小时、分钟、秒、磨,并返回人类可读的版本。
我看到每个人都在发布他们对这个问题的看法,尽管事实上很少有顶级答案已经包含了针对特定用例定制的所有必要信息。
既然我也想赶时髦——下面是我不必要的、有点麻烦的解决方案,那就是:
a)可读性强(希望如此!) b)易于定制 c)不打印任何零
滚筒滚
function durationToDDHHMMSSMS(durms) {
if (!durms) return "??";
var HHMMSSMS = new Date(durms).toISOString().substr(11, 12);
if (!HHMMSSMS) return "??";
var HHMMSS = HHMMSSMS.split(".")[0];
if (!HHMMSS) return "??";
var MS = parseInt(HHMMSSMS.split(".")[1],10);
var split = HHMMSS.split(":");
var SS = parseInt(split[2],10);
var MM = parseInt(split[1],10);
var HH = parseInt(split[0],10);
var DD = Math.floor(durms/(1000*60*60*24));
var string = "";
if (DD) string += ` ${DD}d`;
if (HH) string += ` ${HH}h`;
if (MM) string += ` ${MM}m`;
if (SS) string += ` ${SS}s`;
if (MS) string += ` ${MS}ms`;
return string;
},
注意,这段代码使用ES6模板字符串,我相信像你这样聪明的人在需要时用常规字符串替换它们没有困难。
主题的变奏。处理个位数秒的方式有点不同
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;
}
毫秒到持续时间,简单的方法是:
// 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
const secondsToTime = (seconds, locale) => { const date = new date (0); 日期。setHours(0, 0, seconds, 0); 返回date.toLocaleTimeString(地区); } “en”console.log (secondsToTime (3610));
在哪里区域参数(“en”,“de”等)是可选的