Skip to content

Commit 576e93e

Browse files
authored
fix: Allow customizing "am" / "pm" strings with locale meridiem function (#580)
* fix: Allow customizing "am" / "pm" strings with locale meridiem function fix #578 * test: Add meridiem test * docs: Update meridiem docs
1 parent 0aa7143 commit 576e93e

File tree

10 files changed

+48
-4
lines changed

10 files changed

+48
-4
lines changed

docs/en/I18n.md

+4
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ const localeObject = {
122122
MM: '%d months',
123123
y: 'a year',
124124
yy: '%d years'
125+
},
126+
meridiem: (hour, minute, isLowercase) => {
127+
// OPTIONAL, AM/PM
128+
return hour > 12 ? 'PM' : 'AM'
125129
}
126130
}
127131
```

docs/es-es/I18n.md

+4
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ const localeObject = {
111111
MM: '%d months',
112112
y: 'a year',
113113
yy: '%d years'
114+
},
115+
meridiem: (hour, minute, isLowercase) => {
116+
// OPTIONAL, AM/PM
117+
return hour > 12 ? 'PM' : 'AM'
114118
}
115119
}
116120
```

docs/ja/I18n.md

+4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ const localeObject = {
108108
MM: '%d months',
109109
y: 'a year',
110110
yy: '%d years'
111+
},
112+
meridiem: (hour, minute, isLowercase) => {
113+
// OPTIONAL, AM/PM
114+
return hour > 12 ? 'PM' : 'AM'
111115
}
112116
}
113117
```

docs/ko/I18n.md

+4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ const localeObject = {
107107
MM: '%d months',
108108
y: 'a year',
109109
yy: '%d years'
110+
},
111+
meridiem: (hour, minute, isLowercase) => {
112+
// OPTIONAL, AM/PM
113+
return hour > 12 ? 'PM' : 'AM'
110114
}
111115
}
112116
```

docs/pt-br/I18n.md

+4
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ const objetoLocale = {
122122
MM: '%d months',
123123
y: 'a year',
124124
yy: '%d years'
125+
},
126+
meridiem: (hour, minute, isLowercase) => {
127+
// OPTIONAL, AM/PM
128+
return hour > 12 ? 'PM' : 'AM'
125129
}
126130
}
127131
```

docs/zh-cn/I18n.md

+4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ const localeObject = {
108108
MM: '%d months',
109109
y: 'a year',
110110
yy: '%d years'
111+
},
112+
meridiem: (hour, minute, isLowercase) => {
113+
// 可选, AM/PM
114+
return hour > 12 ? '下午' : '上午'
111115
}
112116
}
113117
```

src/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class Dayjs {
279279
const zoneStr = Utils.z(this)
280280
const locale = this.$locale()
281281
const {
282-
weekdays, months
282+
weekdays, months, meridiem
283283
} = locale
284284
const getShort = (arr, index, full, length) => (
285285
(arr && arr[index]) || full[index].substr(0, length)
@@ -288,6 +288,11 @@ class Dayjs {
288288
Utils.s(this.$H % 12 || 12, num, '0')
289289
)
290290

291+
const meridiemFunc = meridiem || ((hour, minute, isLowercase) => {
292+
const m = (hour < 12 ? 'AM' : 'PM')
293+
return isLowercase ? m.toLowerCase() : m
294+
})
295+
291296
const matches = {
292297
YY: String(this.$y).slice(-2),
293298
YYYY: String(this.$y),
@@ -305,8 +310,8 @@ class Dayjs {
305310
HH: Utils.s(this.$H, 2, '0'),
306311
h: get$H(1),
307312
hh: get$H(2),
308-
a: this.$H < 12 ? 'am' : 'pm',
309-
A: this.$H < 12 ? 'AM' : 'PM',
313+
a: meridiemFunc(this.$H, this.$m, true),
314+
A: meridiemFunc(this.$H, this.$m, false),
310315
m: String(this.$m),
311316
mm: Utils.s(this.$m, 2, '0'),
312317
s: String(this.$s),

src/locale/ja.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const locale = {
1919
lll: 'YYYY年M月D日 HH:mm',
2020
llll: 'YYYY年M月D日(ddd) HH:mm'
2121
},
22+
meridiem: hour => (hour < 12 ? '午前' : '午後'),
2223
relativeTime: {
2324
future: '%s後',
2425
past: '%s前',

test/display.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import moment from 'moment'
22
import MockDate from 'mockdate'
33
import dayjs from '../src'
44
import th from '../src/locale/th'
5+
import '../src/locale/ja'
56

67
beforeEach(() => {
78
MockDate.set(new Date())
@@ -77,12 +78,18 @@ it('Format meridiens a A am / pm', () => {
7778
expect(dayjs(time).format('a')).toBe(moment(time).format('a'))
7879
expect(dayjs(time).format('A')).toBe('AM')
7980
expect(dayjs(time).format('A')).toBe(moment(time).format('A'))
81+
expect(dayjs(time).locale('ja').format('a')).toBe('午前')
82+
expect(dayjs(time).locale('ja').format('a'))
83+
.toBe(moment(time).locale('ja').format('a'))
8084

8185
const time2 = '2018-05-02T23:00:00.000'
8286
expect(dayjs(time2).format('a')).toBe('pm')
8387
expect(dayjs(time2).format('a')).toBe(moment(time2).format('a'))
8488
expect(dayjs(time2).format('A')).toBe('PM')
8589
expect(dayjs(time2).format('A')).toBe(moment(time2).format('A'))
90+
expect(dayjs(time2).locale('ja').format('a')).toBe('午後')
91+
expect(dayjs(time2).locale('ja').format('a'))
92+
.toBe(moment(time2).locale('ja').format('a'))
8693
})
8794

8895
it('Format Minute m mm', () => {

test/locale/keys.test.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ it('Locale keys', () => {
2727
weekdaysShort,
2828
monthsShort,
2929
weekdaysMin,
30-
weekStart
30+
weekStart,
31+
meridiem
3132
} = locale.content
3233

3334
expect(name).toEqual(locale.name.replace('.js', ''))
@@ -80,5 +81,11 @@ it('Locale keys', () => {
8081
'past', 's', 'y', 'yy']
8182
.sort())
8283
}
84+
85+
if (meridiem) {
86+
for (let i = 1; i <= 23; i += 1) {
87+
expect(meridiem(i)).toEqual(expect.anything())
88+
}
89+
}
8390
})
8491
})

0 commit comments

Comments
 (0)