Skip to content

Commit

Permalink
feat(date): add more functions to default adapter (#19141)
Browse files Browse the repository at this point in the history
Co-authored-by: John Leider <john@vuetifyjs.com>
  • Loading branch information
Fo00oX and johnleider authored Mar 6, 2024
1 parent 0f2ea2b commit a6a02f0
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/vuetify/src/composables/date/DateAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ export interface DateAdapter<T = unknown> {
startOfYear (date: T): T
endOfYear (date: T): T

isBefore (date: T, comparing: T): boolean
isAfter (date: T, comparing: T): boolean
isEqual (date: T, comparing: T): boolean
isAfterDay(value: T, comparing: T): boolean

isSameDay (date: T, comparing: T): boolean
isSameMonth (date: T, comparing: T): boolean
isSameYear(value: T, comparing: T): boolean

isBefore (date: T, comparing: T): boolean
isEqual (date: T, comparing: T): boolean
isValid (date: any): boolean
isWithinRange (date: T, range: [T, T]): boolean

Expand All @@ -36,6 +40,8 @@ export interface DateAdapter<T = unknown> {
getMonth (date: T): number
setMonth (date: T, month: number): T
getNextMonth (date: T): T
getPreviousMonth(date: T): T

getHours (date: T): number
setHours (date: T, hours: number): T
getMinutes (date: T): number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,48 @@ describe('vuetify date adapter', () => {

timezoneMock.unregister()
})

describe('isAfterDay', () => {
const dateUtils = new VuetifyDateAdapter({ locale: 'en-us' })

it.each([
[new Date('2024-01-02'), new Date('2024-01-01'), true],
[new Date('2024-02-29'), new Date('2024-02-28'), true],
[new Date('2024-01-01'), new Date('2024-01-01'), false],
[new Date('2024-01-01'), new Date('2024-01-02'), false],
])('returns %s when comparing %s and %s', (date, comparing, expected) => {
expect(dateUtils.isAfterDay(date, comparing)).toBe(expected)
})
})

describe('getPreviousMonth', () => {
const dateUtils = new VuetifyDateAdapter({ locale: 'en-us' })

it.each([
[new Date('2024-03-15'), new Date('2024-02-01'), '2024-03-15 -> 2024-02-01'],
[new Date('2024-01-01'), new Date('2023-12-01'), '2024-01-01 -> 2023-12-01'],
[new Date('2025-01-31'), new Date('2024-12-01'), '2025-01-31 -> 2024-12-01'],
[new Date('2024-02-29'), new Date('2024-01-01'), '2024-02-29 -> 2024-01-01 (Leap Year)'],
[new Date('2023-03-01'), new Date('2023-02-01'), '2023-03-01 -> 2023-02-01'],
])('correctly calculates the first day of the previous month: %s', (date, expected) => {
const result = dateUtils.getPreviousMonth(date)
expect(result.getFullYear()).toBe(expected.getFullYear())
expect(result.getMonth()).toBe(expected.getMonth())
expect(result.getDate()).toBe(expected.getDate())
})
})

describe('isSameYear', () => {
const dateUtils = new VuetifyDateAdapter({ locale: 'en-us' })

it.each([
[new Date('2024-01-01'), new Date('2024-12-31'), true],
[new Date('2024-06-15'), new Date('2024-11-20'), true],
[new Date('2023-01-01'), new Date('2024-01-01'), false],
[new Date('2024-12-31'), new Date('2025-01-01'), false],
[new Date('2024-07-07'), new Date('2023-07-07'), false],
])('returns %s when comparing %s and %s', (date1, date2, expected) => {
expect(dateUtils.isSameYear(date1, date2)).toBe(expected)
})
})
})
24 changes: 24 additions & 0 deletions packages/vuetify/src/composables/date/adapters/vuetify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,10 @@ function getNextMonth (date: Date) {
return new Date(date.getFullYear(), date.getMonth() + 1, 1)
}

function getPreviousMonth (date: Date) {
return new Date(date.getFullYear(), date.getMonth() - 1, 1)
}

function getHours (date: Date) {
return date.getHours()
}
Expand Down Expand Up @@ -408,6 +412,10 @@ function isAfter (date: Date, comparing: Date) {
return date.getTime() > comparing.getTime()
}

function isAfterDay (date: Date, comparing: Date): boolean {
return isAfter(startOfDay(date), startOfDay(comparing))
}

function isBefore (date: Date, comparing: Date) {
return date.getTime() < comparing.getTime()
}
Expand All @@ -427,6 +435,10 @@ function isSameMonth (date: Date, comparing: Date) {
date.getFullYear() === comparing.getFullYear()
}

function isSameYear (date: Date, comparing: Date) {
return date.getFullYear() === comparing.getFullYear()
}

function getDiff (date: Date, comparing: Date | string, unit?: string) {
const d = new Date(date)
const c = new Date(comparing)
Expand Down Expand Up @@ -555,6 +567,10 @@ export class VuetifyDateAdapter implements DateAdapter<Date> {
return isAfter(date, comparing)
}

isAfterDay (date: Date, comparing: Date) {
return isAfterDay(date, comparing)
}

isBefore (date: Date, comparing: Date) {
return !isAfter(date, comparing) && !isEqual(date, comparing)
}
Expand All @@ -567,6 +583,10 @@ export class VuetifyDateAdapter implements DateAdapter<Date> {
return isSameMonth(date, comparing)
}

isSameYear (date: Date, comparing: Date) {
return isSameYear(date, comparing)
}

setMinutes (date: Date, count: number) {
return setMinutes(date, count)
}
Expand Down Expand Up @@ -603,6 +623,10 @@ export class VuetifyDateAdapter implements DateAdapter<Date> {
return getNextMonth(date)
}

getPreviousMonth (date: Date) {
return getPreviousMonth(date)
}

getHours (date: Date) {
return getHours(date)
}
Expand Down

0 comments on commit a6a02f0

Please sign in to comment.