如何使用JavaScript将秒转换为HH-MM-SS字符串?
当前回答
我只是想对上面这个不错的答案做一点解释:
var totalSec = new Date().getTime() / 1000;
var hours = parseInt( totalSec / 3600 ) % 24;
var minutes = parseInt( totalSec / 60 ) % 60;
var seconds = totalSec % 60;
var result = (hours < 10 ? "0" + hours : hours) + "-" + (minutes < 10 ? "0" + minutes : minutes) + "-" + (seconds < 10 ? "0" + seconds : seconds);
On the second line, since there are 3600 seconds in 1 hour, we divide the total number of seconds by 3600 to get the total number of hours. We use parseInt to strip off any decimal. If totalSec was 12600 (3 and half hours), then parseInt( totalSec / 3600 ) would return 3, since we will have 3 full hours. Why do we need the % 24 in this case? If we exceed 24 hours, let's say we have 25 hours (90000 seconds), then the modulo here will take us back to 1 again, rather than returning 25. It is confining the result within a 24 hour limit, since there are 24 hours in one day.
当你看到这样的东西:
25 % 24
你可以这样想:
25 mod 24 or what is the remainder when we divide 25 by 24
其他回答
这是一个非常简单的任务,但你们中的一些人直接推荐了Moment.js,或者创建了一些最丑陋的函数来解析一些秒…
transform(time: number): string {
if (time != null) {
const hours: number = Math.floor(time / 3600);
const minutes: number = Math.floor((time - (hours * 3600)) / 60);
const seconds: number = time - (hours * 3600) - (minutes * 60);
return [hours, (minutes < 10) ? '0' + minutes : minutes, (seconds < 10) ? '0' + seconds : seconds].join(':');
} else {
return '00:00:00';
}
}
这在任何情况下都适用……
在JavaScript Date方法的帮助下,你可以在没有任何外部JavaScript库的情况下做到这一点,如下:
const date = new Date(null);
date.setSeconds(SECONDS); // specify value for SECONDS here
const result = date.toISOString().slice(11, 19);
或者,按照@Frank的评论;一句话:
new Date(SECONDS * 1000).toISOString().slice(11, 19);
更新(2020):
请使用@Frank的一句话解决方案:
new Date(SECONDS * 1000).toISOString().substring(11, 16)
如果SECONDS<3600并且你只想显示MM:SS,那么使用下面的代码:
new Date(SECONDS * 1000).toISOString().substring(14, 19)
这是目前为止最好的解决办法。
旧的回答:
使用Moment.js库。
我认为标准Date对象的任何内置特性都不会以一种比自己做数学更方便的方式为您做这件事。
hours = Math.floor(totalSeconds / 3600);
totalSeconds %= 3600;
minutes = Math.floor(totalSeconds / 60);
seconds = totalSeconds % 60;
例子:
let totalSeconds = 28565; let hours = Math.floor(totalSeconds / 3600); totalSeconds %= 3600; let minutes = Math.floor(totalSeconds / 60); let seconds = totalSeconds % 60; console.log("hours: " + hours); console.log("minutes: " + minutes); console.log("seconds: " + seconds); // If you want strings with leading zeroes: minutes = String(minutes).padStart(2, "0"); hours = String(hours).padStart(2, "0"); seconds = String(seconds).padStart(2, "0"); console.log(hours + ":" + minutes + ":" + seconds);
下面是一个函数,根据powtac的答案将秒转换为hh-mm-ss格式
斯菲德尔
/**
* Convert seconds to hh-mm-ss format.
* @param {number} totalSeconds - the total seconds to convert to hh- mm-ss
**/
var SecondsTohhmmss = function(totalSeconds) {
var hours = Math.floor(totalSeconds / 3600);
var minutes = Math.floor((totalSeconds - (hours * 3600)) / 60);
var seconds = totalSeconds - (hours * 3600) - (minutes * 60);
// round seconds
seconds = Math.round(seconds * 100) / 100
var result = (hours < 10 ? "0" + hours : hours);
result += "-" + (minutes < 10 ? "0" + minutes : minutes);
result += "-" + (seconds < 10 ? "0" + seconds : seconds);
return result;
}
示例使用
var seconds = SecondsTohhmmss(70);
console.log(seconds);
// logs 00-01-10
推荐文章
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 如何格式化Joda-Time DateTime仅为mm/dd/yyyy?
- 使嵌套JavaScript对象平放/不平放的最快方法
- 如何以及为什么'a'['toUpperCase']()在JavaScript工作?
- 有Grunt生成index.html不同的设置
- 文档之间的区别。addEventListener和window。addEventListener?
- 如何检查动态附加的事件监听器是否存在?
- 前一个月的Python日期
- 如何写setTimeout与参数Coffeescript
- 将JavaScript字符串中的多个空格替换为单个空格
- JavaScript: override alert()
- 重置setTimeout
- 如何确保<select>表单字段被禁用时提交?