r/javascript • • Jul 23 '26

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

14 comments sorted by

View all comments

Show parent comment

1

u/Intelligent_Tree6918 Jul 24 '26

i agree with this point, but i write this package for micro-frontend and ui libraries. for eg: if you want separate <span class="val"> and <span class="unit"> elements for custom CSS badges. ui-date gives you structured parts (formattedValue, formattedUnit) out of the box.

1

u/lanerdofchristian Jul 24 '26

Ah, when you nerd-sniped me last night you hadn't added getRelativeTimeParts yet. Here's an implementation using Temporal.ZoneDateTime and Intl.DurationFormat:

function getRelativeTimeParts(time, locale) {
    const dur = time.since(Temporal.Now.zonedDateTimeISO()).round({ smallestUnit: "seconds" })
    const abs = dur.abs()
    const unit = ["years", "months", "days", "hours", "minutes"].find(
        (u) => Temporal.Duration.compare(abs, { [u]: 1 }, { relativeTo: time }) > 0
    ) ?? "seconds"
    const rounded = dur.round({ largestUnit: unit, smallestUnit: unit, relativeTo: time })
    const parts = new Intl.DurationFormat(locale, { style: "long" }).formatToParts(rounded)
    return {
        value: abs[unit],
        unit: unit.slice(0, -1),
        direction: ["past", "present", "future"][1 + dur.sign],
        formattedValue:
            parts.find((a) => a.type === "integer")?.value ??
            rounded.round({ largestUnit: "seconds" }).seconds.toString(),
        formattedUnit: parts.find((a) => a.type === "unit")?.value ?? unit.slice(0, -1),
        formattedText: new Intl.RelativeTimeFormat(locale, { numeric: "auto" })
            .format(rounded[unit], unit),
    }
}

Types are omitted so you can drop this into a compatible console and see how it works.

getRelativeTimeParts(Temporal.Now.zonedDateTimeISO().subtract({ minutes: 15 }))
getRelativeTimeParts(Temporal.Now.zonedDateTimeISO().add({ years: 1 }))

As for the case of separately formatting each part, I'd rather just have the parts directly from the formatter and loop over them in whatever I'm using for templating -- that way I don't run into the issue of only working for English-like languages like you have here. For example, "in 5 minutes" in English is:

in <span class="val">5</span>{/* space! */' '}<span class="unit">minutes</span>

but in Korean it's

<span class="val">5</span>(/* __no__ space! */}<span class="unit">λΆ„</span> ν›„

(or so new Intl.RelativeTimeFormat("ko") tells me).