使用 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