Human Readable Time

题目

Write a function, which takes a non-negative integer (seconds) as input and returns the time in a human-readable format (HH:MM:SS)

1
2
3
HH = hours, padded to 2 digits, range: 00 - 99
MM = minutes, padded to 2 digits, range: 00 - 59
SS = seconds, padded to 2 digits, range: 00 - 59

The maximum time never exceeds 359999 (99:59:59)

分析

注意输出格式是00:00:00

答案

1
2
3
4
5
6
7
8
9
10
11
function humanReadable (seconds) {
let second = seconds % 60
let minute = parseInt(seconds / 60) % 60
let hour = parseInt(seconds / 60 / 60)

function type (x) {
return x < 10 ? '0' + x : x
}

return type(hour) + ':' + type(minute) + ':' + type(second)
}