-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatTimeLength.ts
31 lines (24 loc) · 933 Bytes
/
formatTimeLength.ts
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
30
31
/**
* Format time length.
* @param timeLength The time length in seconds.
* @returns The formatted time length.
*
*/
export default function formatTimeLength(timeLength: number) {
if (timeLength < 60)
return `${timeLength}秒`
const minutes = Math.floor(timeLength / 60)
const seconds = timeLength % 60
if (timeLength < 3600)
return `${minutes}分${seconds}秒`
const hours = Math.floor(timeLength / 3600)
const remainingMinutes = Math.floor((timeLength % 3600) / 60)
const remainingSeconds = timeLength % 60
if (timeLength < 86400)
return `${hours}小时${remainingMinutes}分${remainingSeconds}秒`
const days = Math.floor(timeLength / 86400)
const remainingHours = Math.floor((timeLength % 86400) / 3600)
const finalMinutes = Math.floor((timeLength % 3600) / 60)
const finalSeconds = timeLength % 60
return `${days}天${remainingHours}小时${finalMinutes}分${finalSeconds}秒`
}