我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(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) {
return [
parseInt(seconds / 60 / 60),
parseInt(seconds / 60 % 60),
parseInt(seconds % 60)
]
.join(":")
.replace(/\b(\d)\b/g, "0$1")
}
我个人更喜欢不带前导零的开头单位(天、小时、分钟)。但是秒应该总是以分钟(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>';
看看这小提琴。
使用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);
}
这允许您指定任何持续时间,无论是小时、分钟、秒、磨,并返回人类可读的版本。
我会给artem的答案投票,但我是一个新海报。我确实扩展了他的解决方案,虽然不是OP要求的如下
t=(new Date()).toString().split(" ");
timestring = (t[2]+t[1]+' <b>'+t[4]+'</b> '+t[6][1]+t[7][0]+t[8][0]);
得到
0410月16:31:28太平洋时间
这对我很有用……
但如果你从一个时间量开始,我会用两个函数;一个用于格式化和填充,一个用于计算:
function sec2hms(timect){
if(timect=== undefined||timect==0||timect === null){return ''};
//timect is seconds, NOT milliseconds
var se=timect % 60; //the remainder after div by 60
timect = Math.floor(timect/60);
var mi=timect % 60; //the remainder after div by 60
timect = Math.floor(timect/60);
var hr = timect % 24; //the remainder after div by 24
var dy = Math.floor(timect/24);
return padify (se, mi, hr, dy);
}
function padify (se, mi, hr, dy){
hr = hr<10?"0"+hr:hr;
mi = mi<10?"0"+mi:mi;
se = se<10?"0"+se:se;
dy = dy>0?dy+"d ":"";
return dy+hr+":"+mi+":"+se;
}