Skip to content
Primitives

Relative Time

"2h ago" that auto-updates as time passes and reveals the absolute timestamp on hover. Formatting lives in the shared time system.

Motiontooltip scale-in (shared Tooltip)

Install

Install Kineto and follow the theme setup once in your app.

bash
npm install kineto
tsx
import { RelativeTime } from "kineto/relative-time";

Preview

Posted
Edited
Deployed
Joined
Created

Hover any value for the exact time.

tsx
"use client";

import * as React from "react";

import { cn } from "@/lib/cn";
import {
  formatAbsoluteTime,
  formatRelativeTime,
  relativeUpdateInterval,
  type TimeInput,
} from "@/lib/format-time";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";

export interface RelativeTimeProps
  extends Omit<React.HTMLAttributes<HTMLTimeElement>, "children"> {
  /** The moment to describe — a Date, epoch ms, or parseable date string. */
  date: TimeInput;
  /** Reveal the absolute timestamp in a tooltip on hover/focus. */
  tooltip?: boolean;
  /** Keep the label ticking as time passes. Disable for static/archival contexts. */
  live?: boolean;
}

/**
 * "2h ago" that updates itself as time passes and, on hover/focus, reveals the absolute timestamp.
 * Formatting comes from the shared `format-time` system, so every relative label in the app matches.
 * The auto-update cadence scales with age (seconds tick often, old dates barely at all).
 */
const RelativeTime = React.forwardRef<HTMLTimeElement, RelativeTimeProps>(
  ({ date, tooltip = true, live = true, className, ...props }, ref) => {
    const ms = React.useMemo(() => (date instanceof Date ? date.getTime() : new Date(date).getTime()), [date]);

    // A bare counter to re-render on each tick; the formatters read the live clock themselves.
    const [, tick] = React.useReducer((n: number) => n + 1, 0);
    React.useEffect(() => {
      if (!live) return;
      let timer: ReturnType<typeof setTimeout>;
      const schedule = () => {
        timer = setTimeout(() => {
          tick();
          schedule();
        }, relativeUpdateInterval(ms));
      };
      schedule();
      return () => clearTimeout(timer);
    }, [ms, live]);

    const relative = formatRelativeTime(ms);
    const absolute = formatAbsoluteTime(ms);

    const timeEl = (
      <time
        ref={ref}
        dateTime={new Date(ms).toISOString()}
        className={cn(className)}
        // Server and client clocks differ by the hydration delay; let the text reconcile silently.
        suppressHydrationWarning
        {...props}
      >
        {relative}
      </time>
    );

    if (!tooltip) return timeEl;

    return (
      <Tooltip>
        <TooltipTrigger asChild>{timeEl}</TooltipTrigger>
        <TooltipContent>{absolute}</TooltipContent>
      </Tooltip>
    );
  }
);
RelativeTime.displayName = "RelativeTime";

export { RelativeTime };

src/components/ui/relative-time.tsx

Props

PropTypeDefaultDescription
dateDate | number | string—The moment to describe (Date, epoch ms, or parseable string).
tooltipbooleantrueReveal the absolute timestamp in a tooltip on hover/focus.
livebooleantrueKeep the label ticking; disable for static/archival contexts.