我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)
我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。
那么有一个小片段,这是在jQuery或只是原始JavaScript?
我想转换时间的持续时间,即秒数,以冒号分隔的时间字符串(hh:mm:ss)
我在这里找到了一些有用的答案,但它们都谈到了转换成x小时和x分钟的格式。
那么有一个小片段,这是在jQuery或只是原始JavaScript?
当前回答
const secondsToTime = (seconds, locale) => { const date = new date (0); 日期。setHours(0, 0, seconds, 0); 返回date.toLocaleTimeString(地区); } “en”console.log (secondsToTime (3610));
在哪里区域参数(“en”,“de”等)是可选的
其他回答
我推荐使用普通的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'。
我认为这是目前为止最快的性能:
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
我喜欢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
//secondsToTime();
var t = wachttijd_sec; // your seconds
var hour = Math.floor(t/3600);
if(hour < 10){
hour = '0'+hour;
}
var time = hour+':'+('0'+Math.floor(t/60)%60).slice(-2)+':'+('0' + t % 60).slice(-2);
//would output: 00:00:00 > +100:00:00
即使超过24小时也能保持倒计时
毫秒到持续时间,简单的方法是:
// To have leading zero digits in strings.
function pad(num, size) {
var s = num + "";
while (s.length < size) s = "0" + s;
return s;
}
// ms to time/duration
msToDuration = function(ms){
var seconds = ms / 1000;
var hh = Math.floor(seconds / 3600),
mm = Math.floor(seconds / 60) % 60,
ss = Math.floor(seconds) % 60,
mss = ms % 1000;
return pad(hh,2)+':'+pad(mm,2)+':'+pad(ss,2)+'.'+pad(mss,3);
}
它将327577转换为00:05:27.577。
更新
另一种不同场景的方法:
toHHMMSS = function (n) {
var sep = ':',
n = parseFloat(n),
sss = parseInt((n % 1)*1000),
hh = parseInt(n / 3600);
n %= 3600;
var mm = parseInt(n / 60),
ss = parseInt(n % 60);
return pad(hh,2)+sep+pad(mm,2)+sep+pad(ss,2)+'.'+pad(sss,3);
function pad(num, size) {
var str = num + "";
while (str.length < size) str = "0" + str;
return str;
}
}
toHHMMSS(6315.077) // Return 01:45:15.077