Skip to content

Commit 5eaf77b

Browse files
committed
fix: Fix startOf week bug while week start is not Sunday
1 parent 85396c2 commit 5eaf77b

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

src/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -163,18 +163,18 @@ class Dayjs {
163163
(isStartOf ? argumentStart : argumentEnd).slice(slice)
164164
), this)
165165
}
166-
166+
const { $W, $M, $D } = this
167167
switch (unit) {
168168
case C.Y:
169169
return isStartOf ? instanceFactory(1, 0) :
170170
instanceFactory(31, 11)
171171
case C.M:
172-
return isStartOf ? instanceFactory(1, this.$M) :
173-
instanceFactory(0, this.$M + 1)
172+
return isStartOf ? instanceFactory(1, $M) :
173+
instanceFactory(0, $M + 1)
174174
case C.W: {
175175
const weekStart = this.$locale().weekStart || 0
176-
return isStartOf ? instanceFactory(this.$D - (this.$W - weekStart), this.$M) :
177-
instanceFactory(this.$D + (6 - (this.$W - weekStart)), this.$M)
176+
const gap = ($W < weekStart ? $W + 7 : $W) - weekStart
177+
return instanceFactory(isStartOf ? $D - gap : $D + (6 - gap), $M)
178178
}
179179
case C.D:
180180
case C.DATE:

src/locale/ar.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const locale = {
44
name: 'ar',
55
weekdays: 'الأحد_الإثنين_الثلاثاء_الأربعاء_الخميس_الجمعة_السبت'.split('_'),
66
months: 'يناير_فبراير_مارس_أبريل_مايو_يونيو_يوليو_أغسطس_سبتمبر_أكتوبر_نوفمبر_ديسمبر'.split('_'),
7+
weekStart: 6,
78
relativeTime: {
89
future: 'بعد %s',
910
past: 'منذ %s',

test/manipulate.test.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,15 @@ describe('StartOf EndOf', () => {
3232
})
3333

3434
it('StartOf week with locale', () => {
35-
const testArr = ['zh-cn']
36-
testArr.forEach((l) => {
37-
expect(dayjs().locale(l).startOf('week').date()).toBe(moment().locale(l).startOf('week').date())
38-
expect(dayjs().locale(l).endOf('week').date()).toBe(moment().locale(l).endOf('week').date())
35+
const testDate = [undefined, '2019-02-10', '2019-02-11', '2019-02-12', '2019-02-13', '2019-02-14', '2019-02-15', '2019-02-16']
36+
const testLocale = ['zh-cn', 'ar', 'en']
37+
testDate.forEach((d) => {
38+
testLocale.forEach((l) => {
39+
expect(dayjs(d).locale(l).startOf('week').date())
40+
.toBe(moment(d).locale(l).startOf('week').date())
41+
expect(dayjs(d).locale(l).endOf('week').date())
42+
.toBe(moment(d).locale(l).endOf('week').date())
43+
})
3944
})
4045
})
4146
})

0 commit comments

Comments
 (0)