我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)
我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。
那么有一个小片段,这是在jQuery或只是原始JavaScript?
我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)
我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。
那么有一个小片段,这是在jQuery或只是原始JavaScript?
当前回答
可以使用以下函数将时间转换为HH:MM:SS格式:
var convertTime = function (input, separator) {
var pad = function(input) {return input < 10 ? "0" + input : input;};
return [
pad(Math.floor(input / 3600)),
pad(Math.floor(input % 3600 / 60)),
pad(Math.floor(input % 60)),
].join(typeof separator !== 'undefined' ? separator : ':' );
}
在不传递分隔符的情况下,它使用:作为(默认)分隔符:
time = convertTime(13551.9941351); // --> OUTPUT = 03:45:51
如果你想使用-作为分隔符,只需将其作为第二个参数传递:
time = convertTime(1126.5135155, '-'); // --> OUTPUT = 00-18-46
Demo
var convertTime = function (input, separator) { var pad = function(input) {return input < 10 ? "0" + input : input;}; return [ pad(Math.floor(input / 3600)), pad(Math.floor(input % 3600 / 60)), pad(Math.floor(input % 60)), ].join(typeof separator !== 'undefined' ? separator : ':' ); } document.body.innerHTML = '<pre>' + JSON.stringify({ 5.3515555 : convertTime(5.3515555), 126.2344452 : convertTime(126.2344452, '-'), 1156.1535548 : convertTime(1156.1535548, '.'), 9178.1351559 : convertTime(9178.1351559, ':'), 13555.3515135 : convertTime(13555.3515135, ',') }, null, '\t') + '</pre>';
看看这小提琴。
其他回答
我是这样做的。它似乎工作得相当好,而且非常紧凑。(不过,它使用了很多三元操作符)
function formatTime(seconds) {
var hh = Math.floor(seconds / 3600),
mm = Math.floor(seconds / 60) % 60,
ss = Math.floor(seconds) % 60;
return (hh ? (hh < 10 ? "0" : "") + hh + ":" : "") + ((mm < 10) && hh ? "0" : "") + mm + ":" + (ss < 10 ? "0" : "") + ss
}
...对于格式化字符串…
String.prototype.toHHMMSS = function() {
formatTime(parseInt(this, 10))
};
我认为这是目前为止最快的性能:
var t = 34236; // your seconds
var time = ('0'+Math.floor(t/3600) % 24).slice(-2)+':'+('0'+Math.floor(t/60)%60).slice(-2)+':'+('0' + t % 60).slice(-2)
//would output: 09:30:36
我喜欢第一个答案。 这里有一些优化:
源数据是一个数字。不需要额外的计算。 过多的计算
结果代码:
Number.prototype.toHHMMSS = function () {
var seconds = Math.floor(this),
hours = Math.floor(seconds / 3600);
seconds -= hours*3600;
var minutes = Math.floor(seconds / 60);
seconds -= minutes*60;
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return hours+':'+minutes+':'+seconds;
}
我推荐使用普通的javascript,使用Date对象。(要获得更简短的解决方案,请使用toTimeString,请参阅第二个代码片段。)
var seconds = 9999;
// multiply by 1000 because Date() requires miliseconds
var date = new Date(seconds * 1000);
var hh = date.getUTCHours();
var mm = date.getUTCMinutes();
var ss = date.getSeconds();
// If you were building a timestamp instead of a duration, you would uncomment the following line to get 12-hour (not 24) time
// if (hh > 12) {hh = hh % 12;}
// These lines ensure you have two-digits
if (hh < 10) {hh = "0"+hh;}
if (mm < 10) {mm = "0"+mm;}
if (ss < 10) {ss = "0"+ss;}
// This formats your string to HH:MM:SS
var t = hh+":"+mm+":"+ss;
document.write(t);
(当然,创建的Date对象将有一个与之关联的实际日期,但该数据是无关的,因此出于这些目的,您不必担心它。)
编辑(简短的解决方案):
使用toTimeString函数并对空白进行分割:
var seconds = 9999; // Some arbitrary value
var date = new Date(seconds * 1000); // multiply by 1000 because Date() requires miliseconds
var timeStr = date.toTimeString().split(' ')[0];
toTimeString给出'16:54:58 GMT-0800 (PST)',对第一个空格进行分割给出'16:54:58'。
这很简单,
function toTimeString(seconds) {
return (new Date(seconds * 1000)).toUTCString().match(/(\d\d:\d\d:\d\d)/)[0];
}