-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtimeFormat.ts
More file actions
27 lines (27 loc) · 845 Bytes
/
timeFormat.ts
File metadata and controls
27 lines (27 loc) · 845 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
/**
* 时间格式化
* @param millisecond 毫秒数
* @param format 默认 'hh:mm:ss'
*/
export default function timeFormat(millisecond: number, format = 'hh:mm:ss'): string {
millisecond = +millisecond;
if (isNaN(millisecond)) {
throw new TypeError('millisecond 必须是一个数字');
}
const hours = Math.floor(millisecond / (1000 * 3600));
const minutes = Math.floor((millisecond % (1000 * 3600)) / (1000 * 60));
const seconds = Math.floor(millisecond / 1000) - hours * 3600 - minutes * 60;
return format.replace(/HH|hh|mm|ss/g, matched => {
switch (matched) {
case 'HH':
case 'hh':
return hours.toString().padStart(2, '0');
case 'mm':
return minutes.toString().padStart(2, '0');
case 'ss':
return seconds.toString().padStart(2, '0');
default:
return '';
}
});
}