我想将时间长度(即秒数)转换为冒号分隔的时间字符串(hh:mm:ss)。
我在这里找到了一些有用的答案,但它们都是关于转换为x小时和x分钟格式的。
那么有没有一个小片段可以在jQuery或纯JavaScript中实现此操作?
我想将时间长度(即秒数)转换为冒号分隔的时间字符串(hh:mm:ss)。
我在这里找到了一些有用的答案,但它们都是关于转换为x小时和x分钟格式的。
那么有没有一个小片段可以在jQuery或纯JavaScript中实现此操作?
这很容易。
function toTimeString(seconds) {
return (new Date(seconds * 1000)).toUTCString().match(/(\d\d:\d\d:\d\d)/)[0];
}
Date.prototype.toLocaleTimeString()
的示例。我选择了GB作为语言,因为美国显示初始小时的24
而不是00
。此外,我选择Etc/UTC
作为时区,因为tz数据库时区列表中将UTC
别名为它。
const formatTime = (seconds) =>
new Date(seconds * 1000).toLocaleTimeString('en-GB', {
timeZone:'Etc/UTC',
hour12: false,
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
console.log(formatTime(75)); // 00:01:15
.as-console-wrapper { top: 0; max-height: 100% !important; }
Intl.DateTimeFormat
。这种变体允许您实例化可重复使用的格式化程序对象,性能更好。
const dateFormatter = new Intl.DateTimeFormat('en-GB', {
timeZone:'Etc/UTC',
hour12: false,
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
const formatTime = (seconds) => dateFormatter.format(new Date(seconds * 1000));
console.log(formatTime(75)); // 00:01:15
.as-console-wrapper { top: 0; max-height: 100% !important; }
new Date().toString().split(" ")[4];
结果为 15:08:03
new Date(new Date().getTime() - startTime).toUTCString().split(" ")[4]
,其中startTime
是之前設置的,使用startTime = new Date().getTime();
。(我必須使用toUTCString()
,否則時間會相差一個小時。) - IpsRich最简单的方法来完成它。
new Date(sec * 1000).toISOString().substr(11, 8)
s2t=function (t){
return parseInt(t/86400)+'d '+(new Date(t%86400*1000)).toUTCString().replace(/.*(\d{2}):(\d{2}):(\d{2}).*/, "$1h $2m $3s");
}
s2t(123456);
结果:
1d 10h 17m 36s
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"
我喜欢Powtac的回答,但我想在angular.js中使用它,所以我使用他的代码创建了一个过滤器。
.filter('HHMMSS', ['$filter', function ($filter) {
return function (input, decimals) {
var sec_num = parseInt(input, 10),
decimal = parseFloat(input) - sec_num,
hours = Math.floor(sec_num / 3600),
minutes = Math.floor((sec_num - (hours * 3600)) / 60),
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;
if (decimals > 0) {
time += '.' + $filter('number')(decimal, decimals).substr(2);
}
return time;
};
}])
这个功能与之前的版本相同,唯一的不同就是我增加了一个可选的小数点字段来显示小数秒。像使用其他过滤器一样使用它:
{{ elapsedTime | HHMMSS }}
显示为: 01:23:45
{{ elapsedTime | HHMMSS : 3 }}
显示为: 01:23:45.678
function secToTime(seconds, separator) {
return [
parseInt(seconds / 60 / 60),
parseInt(seconds / 60 % 60),
parseInt(seconds % 60)
].join(separator ? separator : ':')
.replace(/\b(\d)\b/g, "0$1").replace(/^00\:/,'')
}
您现在可以像这样使用:
alert(secToTime("123"));
可运行代码片段:
function secToTime(seconds, separator) {
return [
parseInt(seconds / 60 / 60),
parseInt(seconds / 60 % 60),
parseInt(seconds % 60)
].join(separator ? separator : ':')
.replace(/\b(\d)\b/g, "0$1").replace(/^00\:/,'')
}
console.log(secToTime("123"));
function hms(seconds) {
return [3600, 60]
.reduceRight(
(p, b) => r => [Math.floor(r / b)].concat(p(r % b)),
r => [r]
)(seconds)
.map(a => a.toString().padStart(2, '0'))
.join(':');
}
> hms(0)
< "00:00:00"
> hms(5)
< "00:00:05"
> hms(60)
< "00:01:00"
> hms(3785)
< "01:03:05"
> hms(37850)
< "10:30:50"
> hms(378500)
< "105:08:20"
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"
Math.floor()
,因为它们可能是小数。 (这种情况曾经发生过。) - Winter