js实现将秒数格式化为HH:MM:SS的形式
Posted ErenYeager
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js实现将秒数格式化为HH:MM:SS的形式相关的知识,希望对你有一定的参考价值。
function formatSeconds(value) let secondTime = parseInt(value); let minuteTime = 0; let hourTime = 0; if (secondTime >= 60) minuteTime = parseInt(secondTime / 60); secondTime = parseInt(secondTime % 60); if (minuteTime >= 60) hourTime = parseInt(minuteTime / 60); minuteTime = parseInt(minuteTime % 60); // 补0 hourTime = hourTime < 10 ? "0" + hourTime : hourTime; minuteTime = minuteTime < 10 ? "0" + minuteTime : minuteTime; secondTime = secondTime < 10 ? "0" + secondTime : secondTime; let res = hourTime + ":" + minuteTime + ":" + secondTime; return res; let value = 23453; console.log(formatSeconds(value));
参考文章:
https://blog.csdn.net/weixin_44109827/article/details/126397196
将秒数转为HH:MM:SS格式的时间
/**
* 将秒数转为HH:MM:SS格式的时间
* @param $seconds
* @return string
*/
public static function GetHHMMSSBySeconds($seconds)
{
if ($seconds>3600*24)
{
$hours = intval($seconds/3600);
$leftSeconds = $seconds - $hours* 3600;
$time = sprintf(‘%02s‘,$hours).":".gmstrftime(‘%M:%S‘, $leftSeconds);
}else{
$time = gmstrftime(‘%H:%M:%S‘, $seconds);
}
return $time;
}
以上是关于js实现将秒数格式化为HH:MM:SS的形式的主要内容,如果未能解决你的问题,请参考以下文章