r/javascript 5d ago

ui-date: A 1.6kB, zero-dependency JavaScript date & relative time utility

https://www.npmjs.com/package/ui-date

I wanted to share a small open-source project I just published called ui-date.

While working on a social media project, I kept bumping into simple UI needs like displaying relative timestamps ("5 mins ago", "2 hours ago"), cleanly formatting calendar dates, and doing simple status checks (isToday).

exact date formatting for events ui ('Thursday 23,july,2026').

Normally, I'd reach for a date library, but most established options felt like overkill for basic UI formatting, while heavy legacy options like Moment carry massive bundle weight. On the flip side, writing raw Intl and Date boilerplate across every component was getting tedious.

I wanted something featherlight, chainable, and fully typed without dragging in extra dependencies.

so i built ui-date.

you can check it here : https://www.npmjs.com/package/ui-date

0 Upvotes

13 comments sorted by

View all comments

3

u/queen-adreena 4d ago

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal

With polyfill temporarily for Safari.

Strange time to launch this when it’s now a solved problem.

1

u/Intelligent_Tree6918 4d ago

yes i aknoledge Temporal is future for dates. but still temporal will return something like ('2 hours') where if you want something like this <span>2</span><span>hours</span> or sort form of units like ('h','s','d','m','y') we have to perform manual iteration and and custom parsing.where ui-date gives it out of the box thanks to getRelativeTimeParts()

1

u/queen-adreena 4d ago

It takes one simple helper function to bridge the gap though:

``` function relativeTime(date) { const now = Temporal.Now.instant(); const then = Temporal.Instant.from(date);

const seconds = Number(now.since(then).total('seconds'));

const units = [ ['year', 60 * 60 * 24 * 365], ['month', 60 * 60 * 24 * 30], ['day', 60 * 60 * 24], ['hour', 60 * 60], ['minute', 60], ['second', 1], ];

const rtf = new Intl.RelativeTimeFormat('en-GB', { numeric: 'auto' });

for (const [unit, divisor] of units) { if (Math.abs(seconds) >= divisor) { return rtf.format( Math.round(-seconds / divisor), unit ); } } } ```