我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)
我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。
那么有一个小片段,这是在jQuery或只是原始JavaScript?
我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)
我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。
那么有一个小片段,这是在jQuery或只是原始JavaScript?
当前回答
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"
其他回答
如果你知道你有多少秒,这就可以了。它还使用本机Date()对象。
function formattime(numberofseconds){
var zero = '0', hours, minutes, seconds, time;
time = new Date(0, 0, 0, 0, 0, numberofseconds, 0);
hh = time.getHours();
mm = time.getMinutes();
ss = time.getSeconds()
// Pad zero values to 00
hh = (zero+hh).slice(-2);
mm = (zero+mm).slice(-2);
ss = (zero+ss).slice(-2);
time = hh + ':' + mm + ':' + ss;
return time;
}
我个人更喜欢不带前导零的开头单位(天、小时、分钟)。但是秒应该总是以分钟(0:13)开头,这种表示很容易被认为是“持续时间”,不需要进一步解释(标记为min, sec(s)等),可用于各种语言(国际化)。
// returns (-)d.h:mm:ss(.f)
// (-)h:mm:ss(.f)
// (-)m:ss(.f)
function formatSeconds (value, fracDigits) {
var isNegative = false;
if (isNaN(value)) {
return value;
} else if (value < 0) {
isNegative = true;
value = Math.abs(value);
}
var days = Math.floor(value / 86400);
value %= 86400;
var hours = Math.floor(value / 3600);
value %= 3600;
var minutes = Math.floor(value / 60);
var seconds = (value % 60).toFixed(fracDigits || 0);
if (seconds < 10) {
seconds = '0' + seconds;
}
var res = hours ? (hours + ':' + ('0' + minutes).slice(-2) + ':' + seconds) : (minutes + ':' + seconds);
if (days) {
res = days + '.' + res;
}
return (isNegative ? ('-' + res) : res);
}
//模仿服务器端(.net, c#)持续时间格式:
public static string Format(this TimeSpan interval)
{
string pattern;
if (interval.Days > 0) pattern = @"d\.h\:mm\:ss";
else if (interval.Hours > 0) pattern = @"h\:mm\:ss";
else pattern = @"m\:ss";
return string.Format("{0}", interval.ToString(pattern));
}
下面是一个使用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%重要;}
下面是它的es6版本:
export const parseTime = (time) => { // send time in seconds
// eslint-disable-next-line
let hours = parseInt(time / 60 / 60), mins = Math.abs(parseInt(time / 60) - (hours * 60)), seconds = Math.round(time % 60);
return isNaN(hours) || isNaN(mins) || isNaN(seconds) ? `00:00:00` : `${hours > 9 ? Math.max(hours, 0) : '0' + Math.max(hours, 0)}:${mins > 9 ? Math.max(mins, 0) : '0' + Math.max(0, mins)}:${seconds > 9 ? Math.max(0, seconds) : '0' + Math.max(0, seconds)}`;}
这是最简单的方法。
new Date(sec * 1000).toISOString().substr(11, 8)