helper.ts
2.38 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* @Author: yangming yangming@didihu.com.cn
* @Date: 2023-11-28 14:20:48
* @LastEditors: yangming yangming@didihu.com.cn
* @LastEditTime: 2023-12-15 13:54:33
* @FilePath: \glsx-tracker-javascript\src\utils\helper.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
export default {
dateFormat(timestamp: number | string | Date, format = 'YYYY-MM-DD HH:mm:ss'): string {
var date = new Date(timestamp)
function fixedTwo(value: number): string {
return value < 10 ? '0' + value : String(value)
}
var showTime = format
if (showTime.includes('SSS')) {
const S = date.getMilliseconds()
showTime = showTime.replace('SSS', '0'.repeat(3 - String(S).length) + S)
}
if (showTime.includes('YY')) {
const Y = date.getFullYear()
showTime = showTime.includes('YYYY') ? showTime.replace('YYYY', String(Y)) : showTime.replace('YY', String(Y).slice(2, 4))
}
if (showTime.includes('M')) {
const M = date.getMonth() + 1
showTime = showTime.includes('MM') ? showTime.replace('MM', fixedTwo(M)) : showTime.replace('M', String(M))
}
if (showTime.includes('D')) {
const D = date.getDate()
showTime = showTime.includes('DD') ? showTime.replace('DD', fixedTwo(D)) : showTime.replace('D', String(D))
}
if (showTime.includes('H')) {
const H = date.getHours()
showTime = showTime.includes('HH') ? showTime.replace('HH', fixedTwo(H)) : showTime.replace('H', String(H))
}
if (showTime.includes('m')) {
var m = date.getMinutes()
showTime = showTime.includes('mm') ? showTime.replace('mm', fixedTwo(m)) : showTime.replace('m', String(m))
}
if (showTime.includes('s')) {
var s = date.getSeconds()
showTime = showTime.includes('ss') ? showTime.replace('ss', fixedTwo(s)) : showTime.replace('s', String(s))
}
return showTime
},
trim(str: string) {
return str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '')
},
extend(to: any, _from: any): Object {
for (const key in _from) {
to[key] = _from[key]
}
return to
},
UUID(): string {
return 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/x/g, function (c) {
var r = (Math.random() * 16) | 0,
v = c == 'x' ? r : (r & 0x3) | 0x8
return v.toString(16)
})
}
}