Skip to content

Commit e1c1b1c

Browse files
committed
feat: Added method to retrieve week of the year
1 parent a05e55e commit e1c1b1c

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

src/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ class Dayjs {
120120
return this.$M
121121
}
122122

123+
week() {
124+
const day = this.$W || 7 // Return sunday as 7
125+
// Create date at nearest thursday
126+
const ins = new Date(this.$y, this.$M, (this.$D - day) + 4)
127+
const yearStart = new Date(Date.UTC(this.$y, 0, 1)) // Get first day of year
128+
return Math.ceil((((ins - yearStart) / 86400000) + 1) / 7) // Calculate weeks
129+
}
130+
123131
day() {
124132
return this.$W
125133
}

test/get-set.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ it('Month', () => {
1818
expect(dayjs().month()).toBe(moment().month())
1919
})
2020

21+
it('Week', () => {
22+
expect(dayjs().week()).toBe(moment().week())
23+
})
24+
2125
it('Day of Week', () => {
2226
expect(dayjs().day()).toBe(moment().day())
2327
})

test/index.d.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ dayjs().year()
1616

1717
dayjs().month()
1818

19+
dayjs().week()
20+
1921
dayjs().date()
2022

2123
dayjs().day()

0 commit comments

Comments
 (0)