日常记录#1 毫秒格式化
const timeRE = /([hms])\1?/g
const oneHour = 1000 * 60 * 60
const oneMinute = 1000 * 60
const oneSeconds = 1000
export const FormatTime = (t: number, mask = 'hh:mm:ss'): string => {
let h = Math.floor(t / oneHour)
let m = Math.floor((t % oneHour) / oneMinute)
let s = Math.floor((t % oneMinute) / oneSeconds)
const formats = {
h: h,
hh: pad(h),
H: h == 0 ? '' : h,
HH: h == 0 ? '' : pad(h),
m: m,
mm: pad(m),
s: s,
ss: pad(s)
}
return mask.replace(timeRE, $0 => formats[$0] ?? '')
}
export const pad = (str: string | number, len = 2): string => {
if (typeof str === 'number') {
str = str.toString()
}
while (str.length < len) {
str = '0' + str
}
if (str.length > len) {
str = str.substr(str.length - len)
}
return str
}