-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathhuman-readable-time.js
More file actions
24 lines (20 loc) · 891 Bytes
/
human-readable-time.js
File metadata and controls
24 lines (20 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Test.assertEquals(humanReadable(60), '00:01:00', 'humanReadable(60)');
function humanReadable(totalSeconds) {
const hours = Math.floor(totalSeconds / 3600);
totalSeconds = totalSeconds % 3600;
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
const padNumber = (number) => {
let numberString = number.toString();
if (numberString.length == 1) {
numberString = '0' + numberString;
}
return numberString;
};
return `${padNumber(hours)}:${padNumber(minutes)}:${padNumber(seconds)}`;
}
console.log(humanReadable(0), '00:00:00', 'humanReadable(0)');
console.log(humanReadable(5), '00:00:05', 'humanReadable(5)');
console.log(humanReadable(60), '00:01:00', 'humanReadable(60)');
console.log(humanReadable(86399), '23:59:59', 'humanReadable(86399)');
console.log(humanReadable(359999), '99:59:59', 'humanReadable(359999)');