Skip to content

Commit 2c54c64

Browse files
authored
fix: update advancedFormat plugin to add format options for iso week and weekyear (#1309)
1 parent c5cc893 commit 2c54c64

File tree

8 files changed

+77
-29
lines changed

8 files changed

+77
-29
lines changed

docs/en/Plugin.md

+15-12
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,21 @@ dayjs().format('Q Do k kk X x')
123123

124124
List of added formats:
125125

126-
| Format | Output | Description |
127-
| ------ | --------------------- | ----------------------------------------------------- |
128-
| `Q` | 1-4 | Quarter |
129-
| `Do` | 1st 2nd ... 31st | Day of Month with ordinal |
130-
| `k` | 1-24 | The hour, beginning at 1 |
131-
| `kk` | 01-24 | The hour, 2-digits, beginning at 1 |
132-
| `X` | 1360013296 | Unix Timestamp in second |
133-
| `x` | 1360013296123 | Unix Timestamp in millisecond |
134-
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
135-
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
136-
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
137-
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |
126+
| Format | Output | Description |
127+
| ------ | --------------------- | --------------------------------------------------------------- |
128+
| `Q` | 1-4 | Quarter |
129+
| `Do` | 1st 2nd ... 31st | Day of Month with ordinal |
130+
| `k` | 1-24 | The hour, beginning at 1 |
131+
| `kk` | 01-24 | The hour, 2-digits, beginning at 1 |
132+
| `X` | 1360013296 | Unix Timestamp in second |
133+
| `x` | 1360013296123 | Unix Timestamp in millisecond |
134+
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
135+
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
136+
| `W` | 1 2 ... 52 53 | ISO Week of year (depend: weekOfYear & isoWeek plugin) |
137+
| `WW` | 01 02 ... 52 53 | ISO Week of year, 2-digits (depend: weekOfYear & isoWeek plugin)|
138+
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
139+
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |
140+
| `GGGG` | 2017 | ISO Week Year (depend: weekYear & isoWeek plugin) |
138141

139142
### LocalizedFormat
140143

docs/es-es/Plugin.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,13 @@ Lista de formatos añadidos:
131131
| `kk` | 01-24 | Hora, con 2 dígitos, contando desde 1 |
132132
| `X` | 1360013296 | Tiempo Unix en segundos |
133133
| `x` | 1360013296123 | Tiempo Unix en milisegundos |
134-
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
135-
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
136-
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
137-
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |
134+
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
135+
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
136+
| `W` | 1 2 ... 52 53 | ISO Week of year (depend: weekOfYear & isoWeek plugin) |
137+
| `WW` | 01 02 ... 52 53 | ISO Week of year, 2-digits (depend: weekOfYear & isoWeek plugin)|
138+
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
139+
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |
140+
| `GGGG` | 2017 | ISO Week Year (depend: weekYear & isoWeek plugin) |
138141

139142
### LocalizedFormat
140143

docs/ja/Plugin.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,13 @@ dayjs().format('Q Do k kk X x')
131131
| `kk` | 01-24 | 1 始まりで 2 桁の時間 |
132132
| `X` | 1360013296 | Unix タイムスタンプ (秒) |
133133
| `x` | 1360013296123 | Unix タイムスタンプ (ミリ秒) |
134-
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
135-
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
136-
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
137-
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |
134+
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
135+
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
136+
| `W` | 1 2 ... 52 53 | ISO Week of year (depend: weekOfYear & isoWeek plugin) |
137+
| `WW` | 01 02 ... 52 53 | ISO Week of year, 2-digits (depend: weekOfYear & isoWeek plugin)|
138+
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
139+
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |
140+
| `GGGG` | 2017 | ISO Week Year (depend: weekYear & isoWeek plugin) |
138141

139142
### LocalizedFormat
140143

docs/ko/Plugin.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,13 @@ dayjs().format('Q Do k kk X x')
131131
| `kk` | 01-24 | 시간, 2자리 표현, 1부터 시작 |
132132
| `X` | 1360013296 | 유닉스 타임스템프, 초 |
133133
| `x` | 1360013296123 | 유닉스 타임스탬프, 밀리 초 |
134-
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
135-
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
136-
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
137-
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |
134+
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
135+
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
136+
| `W` | 1 2 ... 52 53 | ISO Week of year (depend: weekOfYear & isoWeek plugin) |
137+
| `WW` | 01 02 ... 52 53 | ISO Week of year, 2-digits (depend: weekOfYear & isoWeek plugin)|
138+
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
139+
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |
140+
| `GGGG` | 2017 | ISO Week Year (depend: weekYear & isoWeek plugin) |
138141

139142
### LocalizedFormat
140143

docs/pt-br/Plugin.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,13 @@ Lista de formatos adicionados:
131131
| `kk` | 01-24 | Hora, com 2 dígitos (começando do 1) |
132132
| `X` | 1360013296 | Unix Timestamp em segundos |
133133
| `x` | 1360013296123 | Unix Timestamp em milissegundos |
134-
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
135-
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
136-
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
137-
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |
134+
| `w` | 1 2 ... 52 53 | Week of year (depend: weekOfYear plugin) |
135+
| `ww` | 01 02 ... 52 53 | Week of year, 2-digits (depend: weekOfYear plugin) |
136+
| `W` | 1 2 ... 52 53 | ISO Week of year (depend: weekOfYear & isoWeek plugin) |
137+
| `WW` | 01 02 ... 52 53 | ISO Week of year, 2-digits (depend: weekOfYear & isoWeek plugin)|
138+
| `wo` | 1st 2nd ... 52nd 53rd | Week of year with ordinal (depend: weekOfYear plugin) |
139+
| `gggg` | 2017 | Week Year (depend: weekYear plugin) |
140+
| `GGGG` | 2017 | ISO Week Year (depend: weekYear & isoWeek plugin) |
138141

139142
### LocalizedFormat
140143

docs/zh-cn/Plugin.md

+3
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,11 @@ dayjs().format('Q Do k kk X x')
132132
| `x` | 1360013296123 | 毫秒单位的 Unix 时间戳 |
133133
| `w` | 1 2 ... 52 53 | 年中第几周 (依赖: weekOfYear 插件) |
134134
| `ww` | 01 02 ... 52 53 | 年中第几周,二位数 (依赖: weekOfYear 插件) |
135+
| `W` | 1 2 ... 52 53 | ISO 年中第几周 (依赖: weekOfYear & isoWeek 插件) |
136+
| `WW` | 01 02 ... 52 53 | ISO 年中第几周,二位数 (依赖: weekOfYear & isoWeek 插件) |
135137
| `wo` | 1st 2nd ... 52nd 53rd | 带序号的年中第几周 (依赖: weekOfYear 插件) |
136138
| `gggg` | 2017 | 根据周计算的年份 (依赖: weekYear 插件) |
139+
| `GGGG` | 2017 | ISO 根据周计算的年份 (依赖: weekYear & isoWeek& isoWeek 插件) |
137140

138141
### LocalizedFormat
139142

src/plugin/advancedFormat/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,24 @@ export default (o, c, d) => { // locale needed later
1313
const locale = this.$locale()
1414
const utils = this.$utils()
1515
const str = formatStr || FORMAT_DEFAULT
16-
const result = str.replace(/\[([^\]]+)]|Q|wo|ww|w|zzz|z|gggg|Do|X|x|k{1,2}|S/g, (match) => {
16+
const result = str.replace(/\[([^\]]+)]|Q|wo|ww|w|WW|W|zzz|z|gggg|GGGG|Do|X|x|k{1,2}|S/g, (match) => {
1717
switch (match) {
1818
case 'Q':
1919
return Math.ceil((this.$M + 1) / 3)
2020
case 'Do':
2121
return locale.ordinal(this.$D)
2222
case 'gggg':
2323
return this.weekYear()
24+
case 'GGGG':
25+
return this.isoWeekYear()
2426
case 'wo':
2527
return locale.ordinal(this.week(), 'W') // W for week
2628
case 'w':
2729
case 'ww':
2830
return utils.s(this.week(), match === 'w' ? 1 : 2, '0')
31+
case 'W':
32+
case 'WW':
33+
return utils.s(this.isoWeek(), match === 'W' ? 1 : 2, '0')
2934
case 'k':
3035
case 'kk':
3136
return utils.s(String(this.$H === 0 ? 24 : this.$H), match === 'k' ? 1 : 2, '0')

test/plugin/advancedFormat.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import MockDate from 'mockdate'
22
import moment from 'moment'
33
import dayjs from '../../src'
44
import advancedFormat from '../../src/plugin/advancedFormat'
5+
import isoWeek from '../../src/plugin/isoWeek'
56
import weekOfYear from '../../src/plugin/weekOfYear'
67
import weekYear from '../../src/plugin/weekYear'
78
import timezone from '../../src/plugin/timezone'
@@ -10,6 +11,7 @@ import '../../src/locale/zh-cn'
1011

1112
dayjs.extend(utc)
1213
dayjs.extend(timezone)
14+
dayjs.extend(isoWeek)
1315
dayjs.extend(weekYear)
1416
dayjs.extend(weekOfYear)
1517
dayjs.extend(advancedFormat)
@@ -82,11 +84,29 @@ it('Format Week of Year wo', () => {
8284
.toBe(moment(d).locale('zh-cn').format('wo'))
8385
})
8486

87+
it('Format Week of Year wo', () => {
88+
const d = '2018-12-01'
89+
expect(dayjs(d).format('wo')).toBe(moment(d).format('wo'))
90+
expect(dayjs(d).locale('zh-cn').format('wo'))
91+
.toBe(moment(d).locale('zh-cn').format('wo'))
92+
})
93+
8594
it('Format Week Year gggg', () => {
8695
const d = '2018-12-31'
8796
expect(dayjs(d).format('gggg')).toBe(moment(d).format('gggg'))
8897
})
8998

99+
it('Format Iso Week Year GGGG', () => {
100+
const d = '2021-01-01'
101+
expect(dayjs(d).format('GGGG')).toBe(moment(d).format('GGGG'))
102+
})
103+
104+
it('Format Iso Week of Year', () => {
105+
const d = '2021-01-01'
106+
expect(dayjs(d).format('W')).toBe(moment(d).format('W'))
107+
expect(dayjs(d).format('WW')).toBe(moment(d).format('WW'))
108+
})
109+
90110
it('Format offsetName z zzz', () => {
91111
const dtz = dayjs.tz('2012-03-11 01:59:59', 'America/New_York')
92112
expect(dtz.format('z')).toBe('EST')
@@ -99,6 +119,11 @@ it('Skips format strings inside brackets', () => {
99119
expect(dayjs().format('[Q]')).toBe('Q')
100120
expect(dayjs().format('[Do]')).toBe('Do')
101121
expect(dayjs().format('[gggg]')).toBe('gggg')
122+
expect(dayjs().format('[GGGG]')).toBe('GGGG')
123+
expect(dayjs().format('[w]')).toBe('w')
124+
expect(dayjs().format('[ww]')).toBe('ww')
125+
expect(dayjs().format('[W]')).toBe('W')
126+
expect(dayjs().format('[WW]')).toBe('WW')
102127
expect(dayjs().format('[wo]')).toBe('wo')
103128
expect(dayjs().format('[k]')).toBe('k')
104129
expect(dayjs().format('[kk]')).toBe('kk')

0 commit comments

Comments
 (0)