Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9e19f34

Browse files
committedMay 15, 2024
feat: localDate/localTime.getAgeIn
1 parent 44d7fb2 commit 9e19f34

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed
 

‎src/datetime/localDate.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ test('basic', () => {
6464
expect(ld.isYoungerThan(5, 'day')).toBe(false)
6565
expect(ld.isOlderThan(100, 'year')).toBe(false)
6666
expect(ld.isYoungerThan(100, 'year')).toBe(true)
67+
expect(ld.getAgeInYears('2024-05-15')).toBe(39)
68+
expect(ld.getAgeInMonths('2024-05-15')).toBe(478)
69+
expect(ld.getAgeInDays('2024-05-15')).toBe(14573)
70+
expect(ld.getAgeIn('day', '2024-05-15')).toBe(14573)
6771
})
6872

6973
test('isBetween', () => {

‎src/datetime/localDate.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ export class LocalDate {
141141
return this.isSameOrAfter(localDate.of(today || new Date()).plus(-n, unit))
142142
}
143143

144+
getAgeInYears(today?: LocalDateInput): number {
145+
return this.getAgeIn('year', today)
146+
}
147+
getAgeInMonths(today?: LocalDateInput): number {
148+
return this.getAgeIn('month', today)
149+
}
150+
getAgeInDays(today?: LocalDateInput): number {
151+
return this.getAgeIn('day', today)
152+
}
153+
getAgeIn(unit: LocalDateUnit, today?: LocalDateInput): number {
154+
return localDate.of(today || new Date()).diff(this, unit)
155+
}
156+
144157
/**
145158
* Returns 1 if this > d
146159
* returns 0 if they are equal

‎src/datetime/localTime.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ test('basic', () => {
4646
expect(lt.isOlderThan(5, 'day')).toBe(true)
4747
expect(lt.isYoungerThan(5, 'day')).toBe(false)
4848
expect(lt.isOlderThan(100, 'year')).toBe(false)
49+
expect(lt.getAgeInYears('2024-05-15')).toBe(2)
50+
expect(lt.getAgeInMonths('2024-05-15')).toBe(28)
51+
expect(lt.getAgeInDays('2024-05-15')).toBe(865)
52+
expect(lt.getAgeIn('day', '2024-05-15')).toBe(865)
4953

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

‎src/datetime/localTime.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,28 @@ export class LocalTime {
500500
return this.isSameOrAfter(localTime.of(now ?? new Date()).plus(-n, unit))
501501
}
502502

503+
getAgeInYears(now?: LocalTimeInput): number {
504+
return this.getAgeIn('year', now)
505+
}
506+
getAgeInMonths(now?: LocalTimeInput): number {
507+
return this.getAgeIn('month', now)
508+
}
509+
getAgeInDays(now?: LocalTimeInput): number {
510+
return this.getAgeIn('day', now)
511+
}
512+
getAgeInHours(now?: LocalTimeInput): number {
513+
return this.getAgeIn('hour', now)
514+
}
515+
getAgeInMinutes(now?: LocalTimeInput): number {
516+
return this.getAgeIn('minute', now)
517+
}
518+
getAgeInSeconds(now?: LocalTimeInput): number {
519+
return this.getAgeIn('second', now)
520+
}
521+
getAgeIn(unit: LocalTimeUnit, now?: LocalTimeInput): number {
522+
return localTime.of(now ?? new Date()).diff(this, unit)
523+
}
524+
503525
/**
504526
* Returns 1 if this > d
505527
* returns 0 if they are equal

0 commit comments

Comments
 (0)
Please sign in to comment.