-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
moment.ts
43 lines (41 loc) 路 1.09 KB
/
moment.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
32
33
34
35
36
37
38
39
40
41
42
43
import { isArr, isFn, isEmpty } from '@formily/shared'
import moment from 'moment'
export const momentable = (value: any, format?: string) => {
return Array.isArray(value)
? value.map((val) => moment(val, format))
: value
? moment(value, format)
: value
}
export const formatMomentValue = (
value: any,
format: any,
placeholder?: string
): string | string[] => {
const formatDate = (date: any, format: any, i = 0) => {
if (!date) return placeholder
const TIME_REG = /^(?:[01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$/
let _format = format
if (isArr(format)) {
_format = format[i]
}
if (isFn(_format)) {
return _format(date)
}
if (isEmpty(_format)) {
return date
}
// moment '19:55:22' 涓嬮渶瑕佷紶鍏ョ浜屼釜鍙傛暟
if (TIME_REG.test(date)) {
return moment(date, _format).format(_format)
}
return moment(date).format(_format)
}
if (isArr(value)) {
return value.map((val, index) => {
return formatDate(val, format, index)
})
} else {
return value ? formatDate(value, format) : value || placeholder
}
}