Skip to content

Commit 0e45b0a

Browse files
g1eny0ungiamkun
authored andcommitted
fix: Handle locale in WeekOfYear plugin (#658)
1 parent 572e5a4 commit 0e45b0a

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/plugin/weekOfYear/index.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@ export default (o, c, d) => {
66
if (week !== null) {
77
return this.add((week - this.week()) * 7, 'day')
88
}
9+
10+
const weekStart = this.$locale().weekStart || 0
11+
912
// d(this) clone is for badMutable plugin
1013
const endOfYear = d(this).endOf(Y)
11-
if (endOfYear.day() !== 6 && this.month() === 11 && (31 - this.date()) <= endOfYear.day()) {
14+
if (
15+
weekStart === 0 &&
16+
endOfYear.day() !== 6 &&
17+
this.month() === 11 &&
18+
31 - this.date() <= endOfYear.day()
19+
) {
1220
return 1
1321
}
22+
1423
const startOfYear = d(this).startOf(Y)
15-
const compareDay = startOfYear.subtract(startOfYear.day(), D).subtract(1, MS)
24+
const compareDay = startOfYear.subtract(startOfYear.day() - weekStart, D).subtract(1, MS)
1625
const diffInWeek = this.diff(compareDay, W, true)
1726
return Math.ceil(diffInWeek)
1827
}

test/plugin/weekOfYear.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import moment from 'moment'
22
import MockDate from 'mockdate'
33
import dayjs from '../../src'
44
import weekOfYear from '../../src/plugin/weekOfYear'
5+
import '../../src/locale/en-gb'
56

67
dayjs.extend(weekOfYear)
78

@@ -14,6 +15,8 @@ afterEach(() => {
1415
})
1516

1617
it('Week of year', () => {
18+
dayjs.locale('en')
19+
1720
const day = '2018-12-31T10:59:09+08:00'
1821
const week = 27
1922
expect(dayjs(day).week()).toBe(moment(day).week())
@@ -24,3 +27,15 @@ it('Week of year', () => {
2427
expect(dayjs().weeks(55).week()).toBe(moment().weeks(55).week())
2528
expect(dayjs().weeks()).toBe(moment().weeks())
2629
})
30+
31+
it('Week of year with locale', () => {
32+
dayjs.locale('en-gb')
33+
moment.locale('en-gb')
34+
35+
const day = '2019-07-28'
36+
expect(dayjs(day).week()).toBe(moment(day).week())
37+
38+
// Edges
39+
expect(dayjs('2018-12-30').week()).toBe(moment('2018-12-30').week())
40+
expect(dayjs('2019-12-29').week()).toBe(moment('2019-12-29').week())
41+
})

0 commit comments

Comments
 (0)