@chirag127/date-utils
v1.0.0
Published
Tiny date utilities — timeAgo, formatDate, fromUnix, toISO
Readme
@chirag127/date-utils
Tiny date utilities. Zero deps. ESM.
Install
npm i @chirag127/date-utilsAPI
All functions accept Date | string | number. Return '' on null/undefined input.
| Function | Returns | Example |
|---|---|---|
| timeAgo(date) | Relative string | '3 hours ago', 'just now' |
| formatDate(date, format?) | Formatted string | 'Jul 8, 2026' |
| fromUnix(seconds) | Date | new Date(seconds * 1000) |
| toISO(date) | ISO 8601 string | '2026-07-08T00:00:00.000Z' |
| isoToRelative(isoString) | Relative string | timeAgo(new Date(isoString)) |
| daysBetween(a, b) | Number of days | Math.abs difference |
formatDate formats
| Format string | Example output |
|---|---|
| (default) | Jul 8, 2026 |
| 'YYYY-MM-DD' | 2026-07-08 |
| 'DD Mon YYYY' | 08 Jul 2026 |
Usage
import { timeAgo, formatDate, fromUnix, toISO, isoToRelative, daysBetween } from '@chirag127/date-utils';
timeAgo(new Date(Date.now() - 3600000)); // '1 hour ago'
formatDate('2026-07-08'); // 'Jul 8, 2026'
formatDate('2026-07-08', 'YYYY-MM-DD'); // '2026-07-08'
fromUnix(1720396800); // Date object
toISO(new Date('2026-07-08')); // '2026-07-08T00:00:00.000Z'
isoToRelative('2026-07-06T10:00:00Z'); // '2 days ago'
daysBetween('2026-07-01', '2026-07-08'); // 7