dobx
v4.0.1
Published
A small, modern utility/tool for working with dates of birth
Downloads
400
Maintainers
Readme
dobx
A small mordern TypeScript-first utility for parsing dates of birth
dob→ "Date of Birth"x→ extension / utility / modern tool
dobx provides strong typing, multi-format parsing and accurate age plus leap year detecton. Perfect for authentication flows, signup forms, profiles and DOB validation
Features
✔ Supports multiple DOB formats:
DD/MM/YYYYDD-MM-YYYYDD.MM.YYYY1st Jan 200001 January 20001 Jan, 2000
✔ Fully type-safe with a strict
DOBStringunion typeDOBStringenforces type safety and allows only valid date formats
✔ Extracts day, month, year
✔ Converts to a real JavaScript
Date✔ Detects leap year
✔ Calculates accurate age
✔ Lightweight and dependency-free
✔ Written in modern TypeScript
Installation
npm install dobx
# or
pnpm add dobx
#or
yarn add dobxUsage
JavaScript Example
import { parseDOB } from "dobx";
const dob = parseDOB("01/09/2003");
console.log(dob);
/*
{
day: 1,
month: 9,
year: 2003,
date: 2003-09-01T00:00:00.000Z,
isLeapYear: false,
age: 22
}
*/TypeScript Example (Type-Safe)
import { parseDOB, type DOBInfo, type DOBString } from "dobx";
const input: DOBString = "1st Sept 2003";
const dob: DOBInfo = parseDOB(input);
console.log(dob.age);Example: Leap Year Handling
import { parseDOB } from "dobx";
const dob = parseDOB("29/02/2000");
console.log(dob.isLeapYear); // true
console.log(dob.age);API
parseDOB(dob: DOBString): DOBInfo
Returns:
| Property | Type | Description |
| ------------ | ------- | ---------------------------------------- |
| day | number | Day of month (1–31) |
| month | number | Month number (1–12) |
| year | number | Full year (e.g., 2000) |
| date | Date | Native JavaScript Date |
| isLeapYear | boolean | Whether the provided year is a leap year |
| age | number | Age calculated from today |
Test Result
License
MIT © thelastofinusa
