1
|
|
|
import dayjs from 'dayjs' |
2
|
|
|
import localizedFormat from 'dayjs/plugin/localizedFormat' |
3
|
|
|
import 'dayjs/locale/en' |
4
|
|
|
import 'dayjs/locale/en-gb' |
5
|
|
|
import 'dayjs/locale/fr' |
6
|
|
|
import 'dayjs/locale/zh-cn' |
7
|
|
|
import 'dayjs/locale/zh-tw' |
8
|
|
|
import 'dayjs/locale/de' |
9
|
|
|
import 'dayjs/locale/es' |
10
|
|
|
import 'dayjs/locale/it' |
11
|
|
|
import 'dayjs/locale/pt' |
12
|
|
|
import 'dayjs/locale/ru' |
13
|
|
|
import 'dayjs/locale/ja' |
14
|
|
|
import 'dayjs/locale/ko' |
15
|
|
|
import 'dayjs/locale/ar' |
16
|
|
|
import 'dayjs/locale/hi' |
17
|
|
|
|
18
|
|
|
const dateLocalObjsDefault = { |
19
|
|
|
L: 'MMMM YYYY', |
20
|
|
|
LLL: 'D MMMM', |
21
|
|
|
LLLL: 'YYYY', |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
const dateLocalObjs = { |
25
|
|
|
de: { |
26
|
|
|
L: 'MMMM YYYY', |
27
|
|
|
LLL: 'D. MMMM', |
28
|
|
|
LLLL: 'YYYY', |
29
|
|
|
}, |
30
|
|
|
es: { |
31
|
|
|
L: 'MMMM [de] YYYY', |
32
|
|
|
LLL: 'D [de] MMMM', |
33
|
|
|
LLLL: 'YYYY', |
34
|
|
|
}, |
35
|
|
|
pt: { |
36
|
|
|
L: 'MMMM [de] YYYY', |
37
|
|
|
LLL: 'D [de] MMMM', |
38
|
|
|
LLLL: 'YYYY', |
39
|
|
|
}, |
40
|
|
|
'zh-cn': { |
41
|
|
|
l: 'YYYY年M月', |
42
|
|
|
lll: 'M月D日', |
43
|
|
|
llll: 'YYYY年', |
44
|
|
|
}, |
45
|
|
|
'zh-tw': { |
46
|
|
|
l: 'YYYY年M月', |
47
|
|
|
lll: 'M月D日', |
48
|
|
|
llll: 'YYYY年', |
49
|
|
|
}, |
50
|
|
|
ja: { |
51
|
|
|
l: 'YYYY年M月', |
52
|
|
|
lll: 'M月D日', |
53
|
|
|
llll: 'YYYY年', |
54
|
|
|
}, |
55
|
|
|
ko: { |
56
|
|
|
l: 'YYYY년 MMMM', |
57
|
|
|
lll: 'MMMM D일', |
58
|
|
|
llll: 'YYYY년', |
59
|
|
|
}, |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
const ageFormats = { |
63
|
|
|
en: 'age *', |
64
|
|
|
'en-gb': 'age *', |
65
|
|
|
fr: '* ans', |
66
|
|
|
'zh-cn': '*岁', |
67
|
|
|
'zh-tw': '*歲', |
68
|
|
|
de: 'Alter *', |
69
|
|
|
es: '* años', |
70
|
|
|
it: '* anni', |
71
|
|
|
pt: '* anos', |
72
|
|
|
ru: '* лет', |
73
|
|
|
ja: '*歳', |
74
|
|
|
ko: '* 세', |
75
|
|
|
ar: '* سنة', |
76
|
|
|
hi: '* साल की उम्र', |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// you should use 'l' and 'lll' at the end |
80
|
|
|
|
81
|
|
|
dayjs.extend(localizedFormat) |
82
|
|
|
|
83
|
|
|
dayjs.extend((option, Dayjs, dayjsp) => { |
84
|
|
|
const dayjsc = dayjsp |
85
|
|
|
const cache = {} |
86
|
|
|
let localeConfig |
87
|
|
|
dayjsc.addFormats = (locale) => { |
88
|
|
|
const localeList = dayjsc.Ls |
89
|
|
|
localeConfig = localeList[locale] |
90
|
|
|
if (!localeConfig) return |
|
|
|
|
91
|
|
|
|
92
|
|
|
let localeObjToAdd |
93
|
|
|
if (locale in dateLocalObjs) { |
94
|
|
|
localeObjToAdd = dateLocalObjs[locale] |
95
|
|
|
} else { |
96
|
|
|
localeObjToAdd = dateLocalObjsDefault |
97
|
|
|
} |
98
|
|
|
Object.keys(localeObjToAdd).forEach((key) => { |
99
|
|
|
cache[key] = localeConfig.formats[key] |
100
|
|
|
}) |
101
|
|
|
|
102
|
|
|
Object.assign(localeConfig.formats, localeObjToAdd) |
103
|
|
|
|
104
|
|
|
return localeConfig // eslint-disable-line consistent-return |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// dayjsc.addFormats.changeBack = () => { |
108
|
|
|
// Object.keys(cache).forEach((key) => { |
109
|
|
|
// localeConfig.formats[key] = cache[key] |
110
|
|
|
// }) |
111
|
|
|
// return localeConfig |
112
|
|
|
// } |
113
|
|
|
}) |
114
|
|
|
|
115
|
|
|
export const my = (date, locale) => { |
116
|
|
|
dayjs.addFormats(locale) |
117
|
|
|
const ret = dayjs(date).locale(locale).format('l') |
118
|
|
|
// dayjs.addFormats.changeBack() |
119
|
|
|
return ret |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// my with first letter being uppercase |
123
|
|
|
export const myUfl = (date, locale) => { |
124
|
|
|
const s = my(date, locale) |
125
|
|
|
return s.charAt(0).toUpperCase() + s.slice(1) |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
export const dm = (date, locale) => { |
129
|
|
|
dayjs.addFormats(locale) |
130
|
|
|
const ret = dayjs(date).locale(locale).format('lll') |
131
|
|
|
// dayjs.addFormats.changeBack() |
132
|
|
|
return ret |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
export const dmy = (date, locale) => dayjs(date).locale(locale).format('ll') |
136
|
|
|
|
137
|
|
|
export const y = (date, locale) => { |
138
|
|
|
dayjs.addFormats(locale) |
139
|
|
|
const ret = dayjs(date).locale(locale).format('llll') |
140
|
|
|
// dayjs.addFormats.changeBack() |
141
|
|
|
return ret |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
export const autoDetectDmy = (date, locale) => { |
145
|
|
|
if (/^\d{4}-\d{1,2}-\d{1,2}$/.test(date)) { |
146
|
|
|
return dmy(date, locale) |
147
|
|
|
} |
148
|
|
|
if (/^\d{4}-\d{1,2}$/.test(date)) { |
149
|
|
|
return myUfl(date, locale) |
150
|
|
|
} |
151
|
|
|
if (/^\d{1,2}-\d{1,2}$/.test(date)) { |
152
|
|
|
return dm(date, locale) |
153
|
|
|
} |
154
|
|
|
if (/^\d{4}$/.test(date)) { |
155
|
|
|
return y(date, locale) |
156
|
|
|
} |
157
|
|
|
return null |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
export const age = (birthDate, locale) => { |
161
|
|
|
const a = dayjs().diff(dayjs(birthDate), 'year') |
162
|
|
|
if (locale in ageFormats) { |
163
|
|
|
return ageFormats[locale].replace('*', a.toString()) |
164
|
|
|
} |
165
|
|
|
return a.toString() |
166
|
|
|
} |
167
|
|
|
|