欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 会展 > js实现格式总时长(如94350秒)为时分秒

js实现格式总时长(如94350秒)为时分秒

2025/1/5 15:37:05 来源:https://blog.csdn.net/fanxingxue/article/details/142956244  浏览:    关键词:js实现格式总时长(如94350秒)为时分秒

1、啰嗦格式方式:秒转为时分秒(94350s  -> 26:12:30 )

let ret = '00:00';
let total = 94350;
if (total < 60) return ret = '00:' + total;
let hour = 0;
let minute = 0;
let second = 0;
if (total > 60 && total < 3600) {minute = parseInt(total / 60);minute = minute < 10 ? '0' + minute : minute;second = parseInt(total % 60);second = second < 10 ? '0' + second : second;ret = minute + ':' + second;return ret;
}
hour = parseInt(total / 3600);
minute = parseInt((total - (hour * 3600)) / 60);
hour = hour < 10 ? '0' + hour : hour;
minute = minute < 10 ? '0' + minute : minute;
second = parseInt(total % 60);
second = second < 10 ? '0' + second : second;
ret = hour + ':' + minute + ':' + second;
return ret;

2、简化形式:秒转为时分秒(94350s  -> 26:12:30)

let ret = '00:00';
let total = 94350;	
var hours = Math.floor(total / 3600);
var minutes = Math.floor(((total % 86400) % 3600) / 60);
var seconds = Math.floor(((total % 86400) % 3600) % 60);
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds :  seconds;
if (hours > 0) {hours = hours < 10 ? '0' + hours : hours;ret = hours + ":" + minutes + ":" + seconds;
}
else ret = minutes + ":" + seconds;
return ret;

3、秒转为天时分秒(94350s  -> 1天  02:12:30)

let ret = '00:00';
let total = 94350;
var days = Math.floor(total / 86400);
var hours = Math.floor((total % 86400) / 3600);
var minutes = Math.floor(((total % 86400) % 3600) / 60);
var seconds = Math.floor(((total % 86400) % 3600) % 60);
if (hours > 0) hours = hours < 10 ? '0' + hours : hours;
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds :  seconds;
if (days > 0)return ret = days + '天' + ' ' + hours + ":" + minutes + ":" + seconds;
if (hours > 0)return ret = hours + ":" + minutes + ":" + seconds;
ret = minutes + ":" + seconds;
return ret;

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com