forked from ant-design/ant-design-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.tsx
31 lines (30 loc) · 840 Bytes
/
utils.tsx
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
function formatIt(date: Date, form: string) {
const pad = (n: number) => (n < 10 ? `0${n}` : n);
const dateStr = `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(
date.getDate(),
)}`;
const timeStr = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
if (form === 'YYYY-MM-DD') {
return dateStr;
}
if (form === 'HH:mm') {
return timeStr;
}
return `${dateStr} ${timeStr}`;
}
export function formatFn(instance: any, value: Date) {
const formatsEnum = {
date: 'YYYY-MM-DD',
time: 'HH:mm',
datetime: 'YYYY-MM-DD HH:mm',
};
const { format } = instance.props;
const type = typeof format;
if (type === 'string') {
return formatIt(value, format);
}
if (type === 'function') {
return format(value);
}
return formatIt(value, (formatsEnum as any)[instance.props.mode]);
}