@awaisbaig/age-calculator
v1.0.3
Published
Universal age calculator library for JavaScript/TypeScript - works with React, Angular, Vue, and all JS frameworks
Maintainers
Readme
Universal Age Calculator
A modern, framework-agnostic age calculator library for JavaScript and TypeScript. Works seamlessly with React, Angular, Vue, Svelte, and vanilla JavaScript. This is an enhanced version of the original @dipaktelangre/age-calculator package, rebuilt from the ground up with modern ESM support.
Features
🌐 Framework Agnostic - Works with React, Angular, Vue, Svelte, and vanilla JS
🎯 TypeScript First - Full TypeScript support with type definitions
📦 Tree-shakeable - Optimized bundle size with ES modules
🧪 Well Tested - Comprehensive unit tests with Vitest
🚀 Easy to Use - Simple and intuitive static API
⚡ Zero Dependencies - Lightweight with no external dependencies
📱 Modern Build - ESM and CommonJS support via tsup
Installation
npm install @awaisbaig/age-calculatorQuick Start
Vanilla JavaScript / TypeScript
import { AgeCalculator } from '@awaisbaig/age-calculator';
// Get age breakdown
const age = AgeCalculator.getAge(new Date('1990-01-01'));
console.log(age); // { years: 34, months: 6, days: 3 }
// Get age in specific unit
const years = AgeCalculator.getAgeIn(new Date('1990-01-01'), 'years');
console.log(years); // 34
// Check if adult
const isAdult = AgeCalculator.isAdult(new Date('1990-01-01'));
console.log(isAdult); // trueAPI Reference
AgeCalculator.getAge(dateOfBirth: Date, currentDate?: Date): AgeResult
Calculate age breakdown in years, months, and days.
const age = AgeCalculator.getAge(new Date('1990-01-01'));
// Returns: { years: 34, months: 6, days: 3 }Parameters:
dateOfBirth- Date of birthcurrentDate- Optional. Current date (defaults to today)
Returns: AgeResult object with years, months, and days
AgeCalculator.getAgeIn(dateOfBirth: Date, unit: AgeUnit, currentDate?: Date): number
Calculate age in a specific unit.
const years = AgeCalculator.getAgeIn(new Date('1990-01-01'), 'years');
// Returns: 34
const months = AgeCalculator.getAgeIn(new Date('1990-01-01'), 'months');
// Returns: 414
const weeks = AgeCalculator.getAgeIn(new Date('1990-01-01'), 'weeks');
// Returns: 1800
const days = AgeCalculator.getAgeIn(new Date('1990-01-01'), 'days');
// Returns: 12600Parameters:
dateOfBirth- Date of birthunit- Unit to calculate:'years'|'months'|'weeks'|'days'|'hours'|'minutes'|'seconds'currentDate- Optional. Current date (defaults to today)
Returns: Age as a number in the specified unit
AgeCalculator.isAdult(dateOfBirth: Date, currentDate?: Date): boolean
Check if a person is an adult (18 years or older).
const adult = AgeCalculator.isAdult(new Date('2000-01-01'));
// Returns: trueAgeCalculator.getNextBirthday(dateOfBirth: Date, currentDate?: Date): Date
Get the date of the next birthday.
const nextBirthday = AgeCalculator.getNextBirthday(new Date('1990-12-25'));
// Returns: Date object for next December 25thAgeCalculator.getDaysUntilNextBirthday(dateOfBirth: Date, currentDate?: Date): number
Get the number of days until the next birthday.
const daysUntil = AgeCalculator.getDaysUntilNextBirthday(new Date('1990-12-25'));
// Returns: number of daysAgeCalculator.getZodiacSign(dateOfBirth: Date): string
Get the zodiac sign based on date of birth.
const sign = AgeCalculator.getZodiacSign(new Date('1990-04-15'));
// Returns: 'Aries'Development
Build the Library
npm run buildRun Tests
npm testWatch Mode
npm run test:watchTypeScript Support
Full TypeScript support with exported types:
import { AgeCalculator, AgeResult, AgeUnit } from '@awaisbaig/age-calculator';
const age: AgeResult = AgeCalculator.getAge(new Date('1990-01-01'));
const unit: AgeUnit = 'years';Bundle Size
- ESM: ~2KB (minified + gzipped)
- CJS: ~2KB (minified + gzipped)
- Zero dependencies
Browser Support
Works in all modern browsers and Node.js 16+:
- Chrome (latest)
- Firefox (latest)
- Safari (latest)
- Edge (latest)
- Node.js 16+
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
MIT © Awais Baig
Credits
Inspired by the original @dipaktelangre/age-calculator package by Dipak Telangre.
Changelog
1.0.0
- Initial release
- Framework-agnostic design
- Full TypeScript support
- ESM and CommonJS support
- Enhanced API with additional methods
- Comprehensive test coverage with Vitest
- Zero dependencies
- Modern build system with tsup
