File tree Expand file tree Collapse file tree 4 files changed +43
-0
lines changed Expand file tree Collapse file tree 4 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -64,6 +64,10 @@ test('basic', () => {
64
64
expect ( ld . isYoungerThan ( 5 , 'day' ) ) . toBe ( false )
65
65
expect ( ld . isOlderThan ( 100 , 'year' ) ) . toBe ( false )
66
66
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 )
67
71
} )
68
72
69
73
test ( 'isBetween' , ( ) => {
Original file line number Diff line number Diff line change @@ -141,6 +141,19 @@ export class LocalDate {
141
141
return this . isSameOrAfter ( localDate . of ( today || new Date ( ) ) . plus ( - n , unit ) )
142
142
}
143
143
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
+
144
157
/**
145
158
* Returns 1 if this > d
146
159
* returns 0 if they are equal
Original file line number Diff line number Diff line change @@ -46,6 +46,10 @@ test('basic', () => {
46
46
expect ( lt . isOlderThan ( 5 , 'day' ) ) . toBe ( true )
47
47
expect ( lt . isYoungerThan ( 5 , 'day' ) ) . toBe ( false )
48
48
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 )
49
53
50
54
expect ( lt . year ( 2023 ) . year ( ) ) . toBe ( 2023 )
51
55
expect ( lt . year ( ) ) . toBe ( 2022 ) // not changed
Original file line number Diff line number Diff line change @@ -500,6 +500,28 @@ export class LocalTime {
500
500
return this . isSameOrAfter ( localTime . of ( now ?? new Date ( ) ) . plus ( - n , unit ) )
501
501
}
502
502
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
+
503
525
/**
504
526
* Returns 1 if this > d
505
527
* returns 0 if they are equal
You can’t perform that action at this time.
0 commit comments