JavaScript 如何将秒转换为时间字符串格式hh:mm:ss
给定一个以秒为单位的时间,任务是将时间转换为字符串格式hh:mm:ss。解决这个问题有两种方法:
方法1:将秒传递给日期对象
- Date() 构造函数会被解析为UNIX时间戳的一种形式之一。UNIX时间戳是自1970年1月1日00:00:00 UTC以来经过的毫秒数。将给定的秒数转换为毫秒,并将其传递给 Date() 构造函数。
- 通过将秒转换为毫秒(乘以 1000 ),可以将时间转换为毫秒的格式,并将其传递给 Date() 构造函数。然后可以使用创建的Date对象根据需要访问小时、分钟和秒。
- 使用 getUTCHours() 方法从日期中提取小时值,使用 getUTCMinutes() 方法从日期中提取UTC时间的分钟值,使用 getSeconds() 方法从日期中提取秒值。
- 通过使用 toString() 方法将这些值中的每一个转换为字符串,并且如果值是个位数,则使用 padStart() 方法在前面添加额外的 “0” ,然后用冒号(:)作为分隔符将各个部分连接在一起。这个字符串的格式就是所需的 “hh:mm:ss” 格式。
语法:
dateObj = new Date(given_seconds * 1000);
hours = dateObj.getUTCHours();
minutes = dateObj.getUTCMinutes();
seconds = dateObj.getSeconds();
timeString = hours.toString().padStart(2, '0') + ':' +
minutes.toString().padStart(2, '0') + ':' +
seconds.toString().padStart(2, '0');
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<title>convert seconds to time string
format hh:mm:ss using JavaScript
</title>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<b>
JavaScript seconds to time
string with format hh:mm:ss
</b>
<p>No. of seconds is: 3685</p>
<p>
Time in hh:mm:ss is:
<span class="output"></span>
</p>
<button onclick="convertSecondstoTime()">
Get time in hh:mm:ss
</button>
<script type="text/javascript">
function convertSecondstoTime() {
given_seconds = 3685;
dateObj = new Date(given_seconds * 1000);
hours = dateObj.getUTCHours();
minutes = dateObj.getUTCMinutes();
seconds = dateObj.getSeconds();
timeString = hours.toString().padStart(2, '0')
+ ':' + minutes.toString().padStart(2, '0')
+ ':' + seconds.toString().padStart(2, '0');
document.querySelector('.output').textContent
= timeString;
}
</script>
</body>
</html>
输出:
方法2:分别计算小时,分钟和秒
- 通过将秒数除以 3600 来计算小时,因为一小时等于3600秒。使用 Math.floor() 函数将该数字取到最近的整数。
- 通过在减去小时数后找到剩下的秒数来计算分钟。将该值除以60得到分钟数。使用 Math.floor() 函数将该数字取到最近的整数。
- 通过从之前的总秒数中减去分钟的秒数和小时的秒数来计算秒数。
- 通过使用 toString() 方法将这些值转换为字符串,并使用 padStart() 方法在值为个位数时填充一个额外的 ‘0’。然后使用冒号(:)将各个部分连接在一起形成最终格式为 ‘hh:mm:ss’ 的字符串。
语法:
hours = Math.floor(given_seconds / 3600);
minutes = Math.floor((given_seconds - (hours * 3600)) / 60);
seconds = given_seconds - (hours * 3600) - (minutes * 60);
timeString = hours.toString().padStart(2, '0') + ':' +
minutes.toString().padStart(2, '0') + ':' +
seconds.toString().padStart(2, '0');
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<title>convert seconds to time string
format hh:mm:ss using JavaScript
</title>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<b>
JavaScript seconds to time
string with format hh:mm:ss
</b>
<p>No. of secomds is: 3685</p>
<p>
Time in hh:mm:ss is:
<span class="output"></span>
</p>
<button onclick="convertSecondstoTime()">
Get time in hh:mm:ss
</button>
<script type="text/javascript">
function convertSecondstoTime() {
given_seconds = 3685;
hours = Math.floor(given_seconds / 3600);
minutes = Math.floor((given_seconds - (hours * 3600)) / 60);
seconds = given_seconds - (hours * 3600) - (minutes * 60);
timeString = hours.toString().padStart(2, '0') + ':' +
minutes.toString().padStart(2, '0') + ':' +
seconds.toString().padStart(2, '0');
document.querySelector('.output').textContent
= timeString;
}
</script>
</body>
</html>
输出: