Skip to content

Commit

Permalink
feat(localDate): add dayOfWeek (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnagydavid authored Mar 26, 2024
1 parent c5fba54 commit a76bea9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/datetime/localDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,14 @@ test('parse weird input', () => {
expect(LocalDate.parseOrNull(Date.now() as any)).toBeNull()
expect(LocalDate.parseOrNull((() => {}) as any)).toBeNull()
})

test('dayOfWeek', () => {
expect(localDate('1984-06-18').dayOfWeek()).toBe(1)
expect(localDate('1984-06-19').dayOfWeek()).toBe(2)
expect(localDate('1984-06-20').dayOfWeek()).toBe(3)
expect(localDate('1984-06-21').dayOfWeek()).toBe(4)
expect(localDate('1984-06-22').dayOfWeek()).toBe(5)
expect(localDate('1984-06-23').dayOfWeek()).toBe(6)
expect(localDate('1984-06-24').dayOfWeek()).toBe(7)
expect(localDate('1984-06-25').dayOfWeek()).toBe(1)
})
6 changes: 5 additions & 1 deletion src/datetime/localDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
UnixTimestampMillisNumber,
UnixTimestampNumber,
} from '../types'
import { LocalTime } from './localTime'
import { ISODayOfWeek, LocalTime } from './localTime'

export type LocalDateUnit = LocalDateUnitStrict | 'week'
export type LocalDateUnitStrict = 'year' | 'month' | 'day'
Expand Down Expand Up @@ -177,6 +177,10 @@ export class LocalDate {
return v === undefined ? this.$day : this.set('day', v)
}

dayOfWeek(): ISODayOfWeek {
return (this.toDate().getDay() || 7) as ISODayOfWeek
}

isSame(d: LocalDateInput): boolean {
d = LocalDate.of(d)
return this.$day === d.$day && this.$month === d.$month && this.$year === d.$year
Expand Down

0 comments on commit a76bea9

Please sign in to comment.