Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow customizing "am" / "pm" strings with locale meridiem function #580

Merged
merged 3 commits into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/en/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ const localeObject = {
MM: '%d months',
y: 'a year',
yy: '%d years'
},
meridiem: (hour, minute, isLowercase) => {
// OPTIONAL, AM/PM
return hour > 12 ? 'PM' : 'AM'
}
}
```
Expand Down
4 changes: 4 additions & 0 deletions docs/es-es/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ const localeObject = {
MM: '%d months',
y: 'a year',
yy: '%d years'
},
meridiem: (hour, minute, isLowercase) => {
// OPTIONAL, AM/PM
return hour > 12 ? 'PM' : 'AM'
}
}
```
Expand Down
4 changes: 4 additions & 0 deletions docs/ja/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ const localeObject = {
MM: '%d months',
y: 'a year',
yy: '%d years'
},
meridiem: (hour, minute, isLowercase) => {
// OPTIONAL, AM/PM
return hour > 12 ? 'PM' : 'AM'
}
}
```
Expand Down
4 changes: 4 additions & 0 deletions docs/ko/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ const localeObject = {
MM: '%d months',
y: 'a year',
yy: '%d years'
},
meridiem: (hour, minute, isLowercase) => {
// OPTIONAL, AM/PM
return hour > 12 ? 'PM' : 'AM'
}
}
```
Expand Down
4 changes: 4 additions & 0 deletions docs/pt-br/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ const objetoLocale = {
MM: '%d months',
y: 'a year',
yy: '%d years'
},
meridiem: (hour, minute, isLowercase) => {
// OPTIONAL, AM/PM
return hour > 12 ? 'PM' : 'AM'
}
}
```
Expand Down
4 changes: 4 additions & 0 deletions docs/zh-cn/I18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ const localeObject = {
MM: '%d months',
y: 'a year',
yy: '%d years'
},
meridiem: (hour, minute, isLowercase) => {
// 可选, AM/PM
return hour > 12 ? '下午' : '上午'
}
}
```
Expand Down
11 changes: 8 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class Dayjs {
const zoneStr = Utils.z(this)
const locale = this.$locale()
const {
weekdays, months
weekdays, months, meridiem
} = locale
const getShort = (arr, index, full, length) => (
(arr && arr[index]) || full[index].substr(0, length)
Expand All @@ -288,6 +288,11 @@ class Dayjs {
Utils.s(this.$H % 12 || 12, num, '0')
)

const meridiemFunc = meridiem || ((hour, minute, isLowercase) => {
const m = (hour < 12 ? 'AM' : 'PM')
return isLowercase ? m.toLowerCase() : m
})

const matches = {
YY: String(this.$y).slice(-2),
YYYY: String(this.$y),
Expand All @@ -305,8 +310,8 @@ class Dayjs {
HH: Utils.s(this.$H, 2, '0'),
h: get$H(1),
hh: get$H(2),
a: this.$H < 12 ? 'am' : 'pm',
A: this.$H < 12 ? 'AM' : 'PM',
a: meridiemFunc(this.$H, this.$m, true),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if you change the name a into am thats readable object or key, wdyt ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think no. This lib is designed to keep the same with moment.js

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it

A: meridiemFunc(this.$H, this.$m, false),
m: String(this.$m),
mm: Utils.s(this.$m, 2, '0'),
s: String(this.$s),
Expand Down
1 change: 1 addition & 0 deletions src/locale/ja.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const locale = {
lll: 'YYYY年M月D日 HH:mm',
llll: 'YYYY年M月D日(ddd) HH:mm'
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it evey locale have this method ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if locale need other than am/pm might need this. moment.js'locale file is a good reference

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, if we have updated documentation too 👍

meridiem: hour => (hour < 12 ? '午前' : '午後'),
relativeTime: {
future: '%s後',
past: '%s前',
Expand Down
7 changes: 7 additions & 0 deletions test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../src'
import th from '../src/locale/th'
import '../src/locale/ja'

beforeEach(() => {
MockDate.set(new Date())
Expand Down Expand Up @@ -77,12 +78,18 @@ it('Format meridiens a A am / pm', () => {
expect(dayjs(time).format('a')).toBe(moment(time).format('a'))
expect(dayjs(time).format('A')).toBe('AM')
expect(dayjs(time).format('A')).toBe(moment(time).format('A'))
expect(dayjs(time).locale('ja').format('a')).toBe('午前')
expect(dayjs(time).locale('ja').format('a'))
.toBe(moment(time).locale('ja').format('a'))

const time2 = '2018-05-02T23:00:00.000'
expect(dayjs(time2).format('a')).toBe('pm')
expect(dayjs(time2).format('a')).toBe(moment(time2).format('a'))
expect(dayjs(time2).format('A')).toBe('PM')
expect(dayjs(time2).format('A')).toBe(moment(time2).format('A'))
expect(dayjs(time2).locale('ja').format('a')).toBe('午後')
expect(dayjs(time2).locale('ja').format('a'))
.toBe(moment(time2).locale('ja').format('a'))
})

it('Format Minute m mm', () => {
Expand Down
9 changes: 8 additions & 1 deletion test/locale/keys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ it('Locale keys', () => {
weekdaysShort,
monthsShort,
weekdaysMin,
weekStart
weekStart,
meridiem
} = locale.content

expect(name).toEqual(locale.name.replace('.js', ''))
Expand Down Expand Up @@ -80,5 +81,11 @@ it('Locale keys', () => {
'past', 's', 'y', 'yy']
.sort())
}

if (meridiem) {
for (let i = 1; i <= 23; i += 1) {
expect(meridiem(i)).toEqual(expect.anything())
}
}
})
})