-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatDuration.js
More file actions
29 lines (26 loc) · 854 Bytes
/
formatDuration.js
File metadata and controls
29 lines (26 loc) · 854 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
25
26
27
28
29
function formatDuration(seconds) {
let duration = '';
let SECONDS_IN_HOUR = 60 * 60;
while (seconds > 0) {
if (seconds > SECONDS_IN_HOUR) {
duration += Math.floor(seconds / SECONDS_IN_HOUR);
duration += ' hour';
seconds = seconds % SECONDS_IN_HOUR;
} else if (seconds > 60) {
if (duration.length > 0) {
duration += ', ';
}
duration += Math.floor(seconds / 60);
duration += ' minute';
seconds = seconds % 60;
} else {
if (duration.length > 0) {
duration += ' and ';
}
duration += `${seconds} second${seconds > 1 ? 's' : ''}`;
seconds = 0;
}
}
return duration;
}
console.log(formatDuration(3662));