Skip to content

Commit 5058623

Browse files
🥇 Human Readable Time
1 parent 60007ef commit 5058623

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

‎humanReadableTime.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Write a function, which takes a non-negative integer (seconds) as input and returns the time in a human-readable format (HH:MM:SS)
2+
3+
// HH = hours, padded to 2 digits, range: 00 - 99
4+
// MM = minutes, padded to 2 digits, range: 00 - 59
5+
// SS = seconds, padded to 2 digits, range: 00 - 59
6+
// The maximum time never exceeds 359999 (99:59:59)
7+
8+
// humanReadable(0), '00:00:00')
9+
// humanReadable(90), '00:01:30')
10+
11+
12+
13+
function humanReadable(seconds) {
14+
return [seconds / 3600, seconds % 3600 / 60, seconds % 60].map(function(v) {
15+
v = Math.floor(v).toString();
16+
return v.length == 1 ? '0' + v : v;
17+
}).join(':');
18+
}
19+
20+
// OR
21+
22+
function humanReadable (seconds) {
23+
let sec = 0, min = 0, hr = 0, rem = 0;
24+
hr = Math.floor(seconds / 3600); if(hr < 10) hr = '0' + hr;
25+
min = Math.floor((seconds - hr*3600) / 60); if(min < 10) min = '0' + min;
26+
sec = seconds - hr*3600 - min*60; if(sec < 10) sec = '0' + sec;
27+
return hr + ':' + min + ':' + sec;
28+
}

0 commit comments

Comments
 (0)