isUnderAge
Checks whether a person is younger than a specific age based on a given birth date.
The inclusive value determines whether the target age itself is included in the comparison.
Code
Interface
typescript
interface IsUnderAgeParams {
birthDate: string | number | Date;
targetAge: number;
inclusive?: boolean;
}
typescript
function isUnderAge({
birthDate,
targetAge,
inclusive = false,
}: IsUnderAgeParams): boolean;
Usage
without inclusive
typescript
import { isUnderAge } from '@modern-kit/utils';
// Based on the current date January 1, 2025
isUnderAge({ birthDate: new Date('2006-01-02'), targetAge: 19 }); // true
isUnderAge({ birthDate: new Date('2006-01-01'), targetAge: 19 }); // false, exactly 19 years old
isUnderAge({ birthDate: new Date('2005-12-31'), targetAge: 19 }); // false
isUnderAge({ birthDate: '2006-01-02', targetAge: 19 }); // true
isUnderAge({ birthDate: '2006-01-01', targetAge: 19 }); // false, exactly 19 years old
isUnderAge({ birthDate: '2005-12-31', targetAge: 19 }); // false
with inclusive
- Determines whether the target age is included in the comparison based on the
inclusivevalue. (default:false) - When
inclusiveistrue, the target age is included; whenfalse, it is not.
typescript
// Based on January 01, 2025
// The inclusive value determines whether to include the target age.
isUnderAge({ birthDate: '2006-01-01', targetAge: 19, inclusive: true }); // true
isUnderAge({ birthDate: '2006-01-01', targetAge: 19, inclusive: false }); // false