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

New display formats #91

Merged
merged 2 commits into from
May 3, 2018
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
16 changes: 15 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class Dayjs {
const weeks = 'Sunday.Monday.Tuesday.Wednesday.Thursday.Friday.Saturday'.split('.')
const months = 'January.February.March.April.May.June.July.August.September.October.November.December'.split('.')

return formatStr.replace(/Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|m{1,2}|s{1,2}|Z{1,2}/g, (match) => {
return formatStr.replace(/Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}/g, (match) => {
switch (match) {
case 'YY':
return String(this.$y).slice(-2)
Expand All @@ -237,6 +237,20 @@ class Dayjs {
return String(this.$H)
case 'HH':
return Utils.padStart(String(this.$H), 2, '0')
case 'h':
if (this.$H === 0) {
return 12
}
return this.$H < 13 ? this.$H : this.$H - 12
case 'hh':
if (this.$H === 0) {
return 12
}
return Utils.padStart(String(this.$H < 13 ? this.$H : this.$H - 12), 2, '0')
case 'a':
return this.$H < 12 ? 'am' : 'pm'
case 'A':
return this.$H < 12 ? 'AM' : 'PM'
case 'm':
return String(this.$m)
case 'mm':
Expand Down
14 changes: 14 additions & 0 deletions test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ it('Format Hour H HH 24-hour', () => {
expect(dayjs().format('HH')).toBe(moment().format('HH'))
})

it('Format Hour h hh 12-hour', () => {
expect(dayjs().format('h')).toBe(moment().format('h'))
expect(dayjs().format('hh')).toBe(moment().format('hh'))

MockDate.set(new Date('2018-05-02T00:00:00.000'))
expect(dayjs().format('h')).toBe(moment().format('h'))
expect(dayjs().format('hh')).toBe(moment().format('hh'))
})

it('Format meridiens a A am / pm', () => {
expect(dayjs().format('a')).toBe(moment().format('a'))
expect(dayjs().format('A')).toBe(moment().format('A'))
})

it('Format Minute m mm', () => {
expect(dayjs().format('m')).toBe(moment().format('m'))
expect(dayjs().format('mm')).toBe(moment().format('mm'))
Expand Down