vue2server7 7.0.104 → 7.0.105
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
function formatDate(input, format = 'YYYY-MM-DD HH:mm:ss') {
|
|
2
|
+
if (!input) return ''
|
|
3
|
+
|
|
4
|
+
let date
|
|
5
|
+
|
|
6
|
+
// Date对象
|
|
7
|
+
if (input instanceof Date) {
|
|
8
|
+
date = input
|
|
9
|
+
}
|
|
10
|
+
// 时间戳
|
|
11
|
+
else if (typeof input === 'number') {
|
|
12
|
+
// 秒级时间戳自动转毫秒
|
|
13
|
+
date = new Date(
|
|
14
|
+
input.toString().length === 10
|
|
15
|
+
? input * 1000
|
|
16
|
+
: input
|
|
17
|
+
)
|
|
18
|
+
}
|
|
19
|
+
// 字符串
|
|
20
|
+
else if (typeof input === 'string') {
|
|
21
|
+
const value = input.trim()
|
|
22
|
+
|
|
23
|
+
// 纯数字字符串时间戳
|
|
24
|
+
if (/^\d+$/.test(value)) {
|
|
25
|
+
date = new Date(
|
|
26
|
+
value.length === 10
|
|
27
|
+
? Number(value) * 1000
|
|
28
|
+
: Number(value)
|
|
29
|
+
)
|
|
30
|
+
} else {
|
|
31
|
+
// 兼容 Safari
|
|
32
|
+
date = new Date(
|
|
33
|
+
value.replace(/-/g, '/')
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
return ''
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// 无效日期
|
|
41
|
+
if (isNaN(date.getTime())) {
|
|
42
|
+
return ''
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const map = {
|
|
46
|
+
YYYY: date.getFullYear(),
|
|
47
|
+
MM: String(date.getMonth() + 1).padStart(2, '0'),
|
|
48
|
+
DD: String(date.getDate()).padStart(2, '0'),
|
|
49
|
+
HH: String(date.getHours()).padStart(2, '0'),
|
|
50
|
+
mm: String(date.getMinutes()).padStart(2, '0'),
|
|
51
|
+
ss: String(date.getSeconds()).padStart(2, '0')
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return format.replace(
|
|
55
|
+
/YYYY|MM|DD|HH|mm|ss/g,
|
|
56
|
+
key => map[key]
|
|
57
|
+
)
|
|
58
|
+
}
|