我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)
我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。
那么有一个小片段,这是在jQuery或只是原始JavaScript?
我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)
我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。
那么有一个小片段,这是在jQuery或只是原始JavaScript?
当前回答
新的日期().toString()。分割(" ")[4];
结果15:08:03
其他回答
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"
我推荐使用普通的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 timeFromSecs(seconds)
{
return(
Math.floor(seconds/86400)+'d :'+
Math.floor(((seconds/86400)%1)*24)+'h : '+
Math.floor(((seconds/3600)%1)*60)+'m : '+
Math.round(((seconds/60)%1)*60)+'s');
}
timeFromSecs(22341938)将返回“258d 14h 5m 38s”
我个人更喜欢不带前导零的开头单位(天、小时、分钟)。但是秒应该总是以分钟(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));
}
可以使用以下函数将时间转换为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>';
看看这小提琴。