使用 JavaScript 从秒数中获取小时和分钟
问题
我们被要求编写一个 JavaScript 函数,其接收秒数并返回这些秒数中所包含的小时数和分钟数。
输入
const seconds = 3601;
输出
const output = "1 hour(s) and 0 minute(s)";
示例
代码如下 −
const seconds = 3601;
const toTime = (seconds = 60) => {
const hR = 3600;
const mR = 60;
let h = parseInt(seconds / hR);
let m = parseInt((seconds - (h * 3600)) / mR);
let res = '';
res += (`${h} hour(s) and ${m} minute(s)`)
return res;
};
console.log(toTime(seconds));输出
"1 hour(s) and 0 minute(s)"
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP