Skip to content
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
12 changes: 10 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,19 @@ class Dayjs {
})

const matches = (match) => {
const year = this.$y
// console.log($W, $M, year)
// if ($W === 1 && $M === 11) {
// year += 1
// }
// if ($M === 0 && $W >= 52) {
// year -= 1
// }
switch (match) {
case 'YY':
return String(this.$y).slice(-2)
return String(year).slice(-2)
case 'YYYY':
return Utils.s(this.$y, 4, '0')
return Utils.s(year, 4, '0')
case 'M':
return $M + 1
case 'MM':
Expand Down
4 changes: 4 additions & 0 deletions src/plugin/weekYear/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import dayjs from '../../index'

export default (o, c) => {
const proto = c.prototype
proto.weekYear = function () {
const month = this.month()
const weekOfYear = this.week()
const year = this.year()
console.log(month, weekOfYear, year)
console.log(dayjs('2025-12-31').format('YYYY年w周'))
if (weekOfYear === 1 && month === 11) {
return year + 1
}
Expand Down
29 changes: 29 additions & 0 deletions test/plugin/weekOfYear.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,32 @@ it('Format w ww wo', () => {
const M = moment(day)
expect(D.format('w ww wo')).toBe(M.format('w ww wo'))
})

it('Issue #2987: Week number after subtracting one week from January 4, 2026', () => {
dayjs.locale('en')
moment.locale('en')
// Subtracting one week from January 4, 2026 should give December 28, 2025
// const result = dayjs('2026-01-04').subtract(1, 'w')
const result = dayjs('2025-12-31')

// Check the format matches moment.js
expect(result.format('YYYY年w周')).toBe('2025年53周')
})

it('Issue #2987: Week number with en-gb locale (yearStart: 4)', () => {
dayjs.locale('en-gb')
moment.locale('en-gb')
// With yearStart: 4, December 28, 2025 should be week 52 of 2025, not week 1
const result = dayjs('2026-01-04').subtract(1, 'w')
const momentResult = moment('2026-01-04').subtract(1, 'w')

// Check the week number matches moment.js
expect(result.week()).toBe(momentResult.week())

// Check the format matches moment.js
expect(result.format('YYYY年w周')).toBe(momentResult.format('YYYY年w周'))

// With en-gb locale, it should be 2025年52周, not 2025年1周
const formatted = result.format('YYYY年w周')
expect(formatted).toBe('2025年52周')
})