Skip to content

Improve ergonomics with RelativeTimeFormat #34

@Josh-Cena

Description

@Josh-Cena

RelativeTimeFormat only takes a single unit, but a duration always contains an arbitrary number of units. This means if I have a duration representing the difference between two points, I need to round it to a fixed unit (both largest and smallest). The typical idea is to round it to "days, or, if the duration in total is less than 1 day, whatever its largest unit is". This is currently extremely unwieldy to do:

const now = Temporal.now.instant();
const emailSent = Temporal.Instant.from("2024-04-20T12:00:00Z");
const duration = emailSent.since(now).round({ largestUnit: "days" });
const unit = ["days", "hours", "minutes", "seconds"].find((u) => duration[u]);
const durationRounded = duration.round({ smallestUnit: unit });
const time = new Intl.RelativeTimeFormat("en-US").format(durationRounded[unit], unit);

I think we can either make a getter like duration.largestUnit, or make RelativeTimeFormat accept a duration object, where it automatically applies said rounding (less ideal because then I can't control the rounding process).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions