diff --git a/src/plugin/advancedFormat/index.js b/src/plugin/advancedFormat/index.js index 9a1e30eb2..1b5288c08 100644 --- a/src/plugin/advancedFormat/index.js +++ b/src/plugin/advancedFormat/index.js @@ -13,12 +13,14 @@ 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|Do|X|x|k{1,2}|S/g, (match) => { + const result = str.replace(/Q|wo|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 'wo': return locale.ordinal(this.week(), 'W') // W for week case 'k': diff --git a/test/plugin/advancedFormat.test.js b/test/plugin/advancedFormat.test.js index 37c185576..74de4eb9b 100644 --- a/test/plugin/advancedFormat.test.js +++ b/test/plugin/advancedFormat.test.js @@ -3,8 +3,10 @@ import moment from 'moment' import dayjs from '../../src' import advancedFormat from '../../src/plugin/advancedFormat' import weekOfYear from '../../src/plugin/weekOfYear' +import weekYear from '../../src/plugin/weekYear' import '../../src/locale/zh-cn' +dayjs.extend(weekYear) dayjs.extend(weekOfYear) dayjs.extend(advancedFormat) @@ -75,3 +77,8 @@ it('Format Week of Year 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')) +})