秒数转时间 分隔符 时、分、秒
32000 s 换算 08小时53分20秒
const formatSeconds = (seconds) => {
let secondTime = parseInt(seconds); // 秒
let minuteTime = 0; // 分
let hourTime = 0; // 小时
if (secondTime > 60) { //如果秒数大于60,将秒数转换成整数
//获取分钟,除以60取整数,得到整数分钟
minuteTime = parseInt(secondTime / 60);
//获取秒数,秒数取余,得到整数秒数
secondTime = parseInt(secondTime % 60);
//如果分钟大于60,将分钟转换成小时
if (minuteTime > 60) {
//获取小时,获取分钟除以60,得到整数小时
hourTime = parseInt(minuteTime / 60);
//获取小时后取余的分,获取分钟除以60取余的分
minuteTime = parseInt(minuteTime % 60);
}
}
let result = '';
if (secondTime >= 0 && secondTime < 10) {
result = "0" + parseInt(secondTime) + "秒";
} else {
result = "" + parseInt(secondTime) + "秒";
}
if (minuteTime >= 0 && minuteTime < 10) {
result = "0" + parseInt(minuteTime) + "分" + result;
} else {
result = "" + parseInt(minuteTime) + "分" + result;
}
if (hourTime >= 0 && hourTime < 10) {
result = "0" + parseInt(hourTime) + "小时" + result;
} else {
result = "" + parseInt(hourTime) + "小时" + result;
}
return result;
}
let time = formatSeconds(32000)
console.log(time) // 08小时53分20秒
秒转时间 分隔符 :
const getSeconds = (seconds) => {
let secondTime = parseInt(seconds); // 秒
let minuteTime = 0; // 分
let hourTime = 0; // 时
if (secondTime > 60) { //如果秒数大于60,将秒数转换成整数
//获取分钟,除以60取整数,得到整数分钟
minuteTime = parseInt(secondTime / 60);
//获取秒数,秒数取佘,得到整数秒数
secondTime = parseInt(secondTime % 60);
//如果分钟大于60,将分钟转换成小时
if (minuteTime > 60) {
//获取小时,获取分钟除以60,得到整数小时
hourTime = parseInt(minuteTime / 60);
//获取小时后取佘的分,获取分钟除以60取佘的分
minuteTime = parseInt(minuteTime % 60);
}
}
let result = '';
if (secondTime >= 0 && secondTime < 10) {
result = "0" + parseInt(secondTime) + "";
} else {
result = "" + parseInt(secondTime) + "";
}
if (minuteTime >= 0 && minuteTime < 10) {
result = "0" + parseInt(minuteTime) + ":" + result;
} else {
result = "" + parseInt(minuteTime) + ":" + result;
}
if (hourTime >= 0 && hourTime < 10) {
result = "0" + parseInt(hourTime) + ":" + result;
} else {
result = "" + parseInt(hourTime) + ":" + result;
}
return result;
}
let time = getSeconds(32000)
console.log(time) // 08:53:20
两个的原理是一样的,只有分割符不一样,如有不对,请不吝赐教