Skip to content

Commit

Permalink
fix(date): startOfWeek/endOfWeek does not respect locale (#19587)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekosaur authored Apr 10, 2024
1 parent f90ada3 commit 2cb1ae5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,44 @@ describe('vuetify date adapter', () => {

timezoneMock.unregister()
})

it('returns correct start of week', () => {
let instance = new VuetifyDateAdapter({ locale: 'en-US' })

let date = instance.startOfWeek(new Date(2024, 3, 10, 12, 0, 0))

expect(date?.getFullYear()).toBe(2024)
expect(date?.getMonth()).toBe(3)
expect(date?.getDate()).toBe(7)
expect(date?.getDay()).toBe(0)

instance = new VuetifyDateAdapter({ locale: 'sv-SE' })

date = instance.startOfWeek(new Date(2024, 3, 10, 12, 0, 0))

expect(date?.getFullYear()).toBe(2024)
expect(date?.getMonth()).toBe(3)
expect(date?.getDate()).toBe(8)
expect(date?.getDay()).toBe(1)
})

it('returns correct end of week', () => {
let instance = new VuetifyDateAdapter({ locale: 'en-US' })

let date = instance.endOfWeek(new Date(2024, 3, 10, 12, 0, 0))

expect(date?.getFullYear()).toBe(2024)
expect(date?.getMonth()).toBe(3)
expect(date?.getDate()).toBe(13)
expect(date?.getDay()).toBe(6)

instance = new VuetifyDateAdapter({ locale: 'sv-SE' })

date = instance.endOfWeek(new Date(2024, 3, 10, 12, 0, 0))

expect(date?.getFullYear()).toBe(2024)
expect(date?.getMonth()).toBe(3)
expect(date?.getDate()).toBe(14)
expect(date?.getDay()).toBe(0)
})
})
13 changes: 7 additions & 6 deletions packages/vuetify/src/composables/date/adapters/vuetify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,18 @@ function getWeekArray (date: Date, locale: string) {
return weeks
}

function startOfWeek (date: Date) {
function startOfWeek (date: Date, locale: string) {
const d = new Date(date)
while (d.getDay() !== 0) {
while (d.getDay() !== (firstDay[locale.slice(-2).toUpperCase()] ?? 0)) {
d.setDate(d.getDate() - 1)
}
return d
}

function endOfWeek (date: Date) {
function endOfWeek (date: Date, locale: string) {
const d = new Date(date)
while (d.getDay() !== 6) {
const lastDay = ((firstDay[locale.slice(-2).toUpperCase()] ?? 0) + 6) % 7
while (d.getDay() !== lastDay) {
d.setDate(d.getDate() + 1)
}
return d
Expand Down Expand Up @@ -546,11 +547,11 @@ export class VuetifyDateAdapter implements DateAdapter<Date> {
}

startOfWeek (date: Date): Date {
return startOfWeek(date)
return startOfWeek(date, this.locale)
}

endOfWeek (date: Date): Date {
return endOfWeek(date)
return endOfWeek(date, this.locale)
}

startOfMonth (date: Date) {
Expand Down

0 comments on commit 2cb1ae5

Please sign in to comment.