我想将时间长度(即秒数)转换为冒号分隔的时间字符串(hh:mm:ss)。
我在这里找到了一些有用的答案,但它们都是关于转换为x小时和x分钟格式的。
那么有没有一个小片段可以在jQuery或纯JavaScript中实现此操作?
我想将时间长度(即秒数)转换为冒号分隔的时间字符串(hh:mm:ss)。
我在这里找到了一些有用的答案,但它们都是关于转换为x小时和x分钟格式的。
那么有没有一个小片段可以在jQuery或纯JavaScript中实现此操作?
/**
* Formats seconds (number) to H:i:s format.
* 00:12:00
*
* When "short" option is set to true, will return:
* 0:50
* 2:00
* 12:00
* 1:00:24
* 10:00:00
*/
export default function formatTimeHIS (seconds, { short = false } = {}) {
const pad = num => num < 10 ? `0${num}` : num
const H = pad(Math.floor(seconds / 3600))
const i = pad(Math.floor(seconds % 3600 / 60))
const s = pad(seconds % 60)
if (short) {
let result = ''
if (H > 0) result += `${+H}:`
result += `${H > 0 ? i : +i}:${s}`
return result
} else {
return `${H}:${i}:${s}`
}
}
secToHHMM(number: number) {
debugger;
let hours = Math.floor(number / 3600);
let minutes = Math.floor((number - (hours * 3600)) / 60);
let seconds = number - (hours * 3600) - (minutes * 60);
let H, M, S;
if (hours < 10) H = ("0" + hours);
if (minutes < 10) M = ("0" + minutes);
if (seconds < 10) S = ("0" + seconds);
return (H || hours) + ':' + (M || minutes) + ':' + (S || seconds);
}
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'
这是我的解决方案的构想。您可以尝试下面的代码片段。
function secToHHMM(sec) {
var d = new Date();
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);
d = new Date(d.getTime() + sec*1000);
return d.toLocaleString('en-GB').split(' ')[1];
};
alert( 'One hour: ' + secToHHMM(60*60) ); // '01:00:00'
alert( 'One hour five minutes: ' + secToHHMM(60*60 + 5*60) ); // '01:05:00'
alert( 'One hour five minutes 23 seconds: ' + secToHHMM(60*60 + 5*60 + 23) ); // '01:05:23'
你可以使用Momement.js和moment-duration-format插件:
var seconds = 3820;
var duration = moment.duration(seconds, 'seconds');
var formatted = duration.format("hh:mm:ss");
console.log(formatted); // 01:03:40
<!-- Moment.js library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<!-- moment-duration-format plugin -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>
还可以参考这个Fiddle。
//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
function secondsToTime(secs)
{
var hours = Math.floor(secs / (60 * 60));
var divisor_for_minutes = secs % (60 * 60);
var minutes = Math.floor(divisor_for_minutes / 60);
var divisor_for_seconds = divisor_for_minutes % 60;
var seconds = Math.ceil(divisor_for_seconds);
if(hours >= 12)
{
var m= 'pm' ;
}
else
{
var m='am'
}
if(hours-12 >0)
{
var hrs = hours-12;
}
else if(hours-12 <0)
{
var hrs = hours;
}
var obj = {
"h": hrs,
"m": minutes,
"s": seconds,
"a":m
};
return obj;
}
var d = new Date();
var n = d.getHours();
var hms = d.getHours()+':'+d.getMinutes()+':'+d.getSeconds(); // your input string
var a = hms.split(':'); // split it at the colons
// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
console.log(seconds);
console.log(secondsToTime(seconds))
这是我最终采用的@meiyang 优秀解决方案 的一个变体:
function duration(seconds) {
return [
format(seconds / 60 / 60),
format(seconds / 60 % 60),
format(seconds % 60)
].join(':');
}
format(n) {
return (~~n).toString().padStart(2, '0')
}
PS:值得注意的是,上面提供的其他解决方案只适用于小于24小时的值。
我想给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]);
获取
04Oct 16:31:28 PDT
这对我来说有效...
但如果你只有一个时间数量,我会使用两个函数;一个用于格式化和填充,另一个用于计算:
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;
}