Skip to content

Commit

Permalink
fix: Add format options for iso week and weekyear
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Schaffernak committed Jan 4, 2021
1 parent db2b6a5 commit e91b212
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/plugin/advancedFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,24 @@ export default (o, c, d) => { // locale needed later
const locale = this.$locale()
const utils = this.$utils()
const str = formatStr || FORMAT_DEFAULT
const result = str.replace(/\[([^\]]+)]|Q|wo|ww|w|zzz|z|gggg|Do|X|x|k{1,2}|S/g, (match) => {
const result = str.replace(/\[([^\]]+)]|Q|wo|ww|w|WW|W|zzz|z|gggg|GGGG|Do|X|x|k{1,2}|S/g, (match) => {
switch (match) {
case 'Q':
return Math.ceil((this.$M + 1) / 3)
case 'Do':
return locale.ordinal(this.$D)
case 'gggg':
return this.weekYear()
case 'GGGG':
return this.isoWeekYear()
case 'wo':
return locale.ordinal(this.week(), 'W') // W for week
case 'w':
case 'ww':
return utils.s(this.week(), match === 'w' ? 1 : 2, '0')
case 'W':
case 'WW':
return utils.s(this.isoWeek(), match === 'W' ? 1 : 2, '0')
case 'k':
case 'kk':
return utils.s(String(this.$H === 0 ? 24 : this.$H), match === 'k' ? 1 : 2, '0')
Expand Down
25 changes: 25 additions & 0 deletions test/plugin/advancedFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import advancedFormat from '../../src/plugin/advancedFormat'
import isoWeek from '../../src/plugin/isoWeek'
import weekOfYear from '../../src/plugin/weekOfYear'
import weekYear from '../../src/plugin/weekYear'
import timezone from '../../src/plugin/timezone'
Expand All @@ -10,6 +11,7 @@ import '../../src/locale/zh-cn'

dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(isoWeek)
dayjs.extend(weekYear)
dayjs.extend(weekOfYear)
dayjs.extend(advancedFormat)
Expand Down Expand Up @@ -82,11 +84,29 @@ it('Format Week of Year wo', () => {
.toBe(moment(d).locale('zh-cn').format('wo'))
})

it('Format Week of Year wo', () => {
const d = '2018-12-01'
expect(dayjs(d).format('wo')).toBe(moment(d).format('wo'))
expect(dayjs(d).locale('zh-cn').format('wo'))
.toBe(moment(d).locale('zh-cn').format('wo'))
})

it('Format Week Year gggg', () => {
const d = '2018-12-31'
expect(dayjs(d).format('gggg')).toBe(moment(d).format('gggg'))
})

it('Format Iso Week Year GGGG', () => {
const d = '2021-01-01'
expect(dayjs(d).format('GGGG')).toBe(moment(d).format('GGGG'))
})

it('Format Iso Week of Year', () => {
const d = '2021-01-01'
expect(dayjs(d).format('W')).toBe(moment(d).format('W'))
expect(dayjs(d).format('WW')).toBe(moment(d).format('WW'))
})

it('Format offsetName z zzz', () => {
const dtz = dayjs.tz('2012-03-11 01:59:59', 'America/New_York')
expect(dtz.format('z')).toBe('EST')
Expand All @@ -99,6 +119,11 @@ it('Skips format strings inside brackets', () => {
expect(dayjs().format('[Q]')).toBe('Q')
expect(dayjs().format('[Do]')).toBe('Do')
expect(dayjs().format('[gggg]')).toBe('gggg')
expect(dayjs().format('[GGGG]')).toBe('GGGG')
expect(dayjs().format('[w]')).toBe('w')
expect(dayjs().format('[ww]')).toBe('ww')
expect(dayjs().format('[W]')).toBe('W')
expect(dayjs().format('[WW]')).toBe('WW')
expect(dayjs().format('[wo]')).toBe('wo')
expect(dayjs().format('[k]')).toBe('k')
expect(dayjs().format('[kk]')).toBe('kk')
Expand Down

0 comments on commit e91b212

Please sign in to comment.