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

feat: add 2 digit Version (gg and GG) for WeekYear and ISOWeekYear #2822

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion src/plugin/advancedFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ export default (o, c) => { // locale needed later

const utils = this.$utils()
const str = formatStr || FORMAT_DEFAULT
const result = str.replace(/\[([^\]]+)]|Q|wo|ww|w|WW|W|zzz|z|gggg|GGGG|Do|X|x|k{1,2}|S/g, (match) => {
const result = str.replace(/\[([^\]]+)]|Q|wo|ww|w|WW|W|zzz|z|gggg|gg|GGGG|GG|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 'gg':
return String(this.weekYear()).slice(-2)
case 'gggg':
return this.weekYear()
case 'GG':
return String(this.isoWeekYear()).slice(-2)
case 'GGGG':
return this.isoWeekYear()
case 'wo':
Expand Down
10 changes: 10 additions & 0 deletions test/plugin/advancedFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ it('Format Week of Year wo', () => {
.toBe(moment(d).locale('zh-cn').format('wo'))
})

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

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

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