Age calculations and birthday utilities
Import
import { ... } from 'ts-time-utils/age'; Examples
Age & Birthday
Calculate ages and work with birthdays
import { calculateAge, getLifeStage, getNextBirthday, getDaysUntilBirthday } from 'ts-time-utils/age';
const birthDate = new Date('1990-05-15');
// Calculate detailed age
const age = calculateAge(birthDate);
console.log(`Age: ${age.years} years, ${age.months} months, ${age.days} days`);
// "Age: 35 years, 4 months, 2 days"
console.log(`Total months: ${age.totalMonths}`);
console.log(`Total days: ${age.totalDays}`);
// Get life stage
console.log(getLifeStage(5)); // "child"
console.log(getLifeStage(15)); // "teenager"
console.log(getLifeStage(25)); // "adult"
console.log(getLifeStage(65)); // "senior"
// Next birthday
const nextBday = getNextBirthday(birthDate);
console.log('Next birthday:', nextBday.toDateString());
// Days until birthday
const daysLeft = getDaysUntilBirthday(birthDate);
console.log(`Days until birthday: ${daysLeft}`);
// Check if birthday is today
const today = new Date();
const isBirthdayToday = calculateAge(birthDate, today).months === 0
&& calculateAge(birthDate, today).days === 0;
console.log('Birthday today?', isBirthdayToday);