Skip to content

Commit

Permalink
feat: localDate.isToday(), isAfterToday and similar helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Jul 24, 2024
1 parent 3c3071f commit b37a616
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/datetime/localDate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ test('basic', () => {
expect(ld.getAgeInMonths('2024-05-15')).toBe(478)
expect(ld.getAgeInDays('2024-05-15')).toBe(14573)
expect(ld.getAgeIn('day', '2024-05-15')).toBe(14573)

expect(ld.isToday()).toBe(false)
expect(ld.isAfterToday()).toBe(false)
expect(ld.isSameOrAfterToday()).toBe(false)
expect(ld.isBeforeToday()).toBe(true)
expect(ld.isSameOrBeforeToday()).toBe(true)
})

test('constructors', () => {
Expand Down
16 changes: 16 additions & 0 deletions src/datetime/localDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,22 @@ export class LocalDate {
return this.isSameOrAfter(localDate.fromInput(today || new Date()).plus(-n, unit))
}

isToday(): boolean {
return this.isSame(localDate.today())
}
isAfterToday(): boolean {
return this.isAfter(localDate.today())
}
isSameOrAfterToday(): boolean {
return this.isSameOrAfter(localDate.today())
}
isBeforeToday(): boolean {
return this.isBefore(localDate.today())
}
isSameOrBeforeToday(): boolean {
return this.isSameOrBefore(localDate.today())
}

getAgeInYears(today?: LocalDateInput): number {
return this.getAgeIn('year', today)
}
Expand Down
3 changes: 3 additions & 0 deletions src/datetime/localTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ test('basic', () => {
expect(lt.getAgeInDays('2024-05-15')).toBe(865)
expect(lt.getAgeIn('day', '2024-05-15')).toBe(865)

expect(lt.isAfterNow()).toBe(false)
expect(lt.isBeforeNow()).toBe(true)

expect(lt.setYear(2023).year).toBe(2023)
expect(lt.year).toBe(2022) // not changed

Expand Down
7 changes: 7 additions & 0 deletions src/datetime/localTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,13 @@ export class LocalTime {
return localTime.fromInput(now ?? new Date()).diff(this, unit)
}

isAfterNow(): boolean {
return this.$date.valueOf() > Date.now()
}
isBeforeNow(): boolean {
return this.$date.valueOf() < Date.now()
}

/**
* Returns 1 if this > d
* returns 0 if they are equal
Expand Down

0 comments on commit b37a616

Please sign in to comment.