Skip to content

useTimeAgo

Returns a ReadonlyObservable<string> with a human-readable relative time string (e.g. “5 minutes ago”, “in 2 days”) that auto-updates at a configurable interval.

Formatting is delegated to date-fns/formatDistance, enabling full i18n locale support.

Also exports formatTimeAgo as a standalone pure function for one-off formatting.

just now
0ms
import {
import useTimeAgo
useTimeAgo
} from "@usels/web";
const
const timeAgo: any
timeAgo
=
import useTimeAgo
useTimeAgo
(new
any
Date
("2024-01-01"));
// timeAgo.get() → "about 3 months ago"
import {
import useTimeAgo
useTimeAgo
} from "@usels/web";
import {
function observable<T>(): Observable<T | undefined> (+2 overloads)
observable
} from "@legendapp/state";
const
const time$: any
time$
=
observable<unknown>(value: Promise<unknown> | (() => unknown) | unknown): any (+2 overloads)
observable
(new
any
Date
());
const
const timeAgo: any
timeAgo
=
import useTimeAgo
useTimeAgo
(
const time$: any
time$
);
const time$: any
time$
.
any
set
(new
any
Date
("2020-01-01")); // → "about 4 years ago"
import {
import useTimeAgo
useTimeAgo
} from "@usels/web";
const {
const timeAgo$: any
timeAgo$
,
const isActive$: any
isActive$
,
const pause: any
pause
,
const resume: any
resume
} =
import useTimeAgo
useTimeAgo
(new
any
Date
(), {
controls: boolean
controls
: true,
});
import {
import useTimeAgo
useTimeAgo
} from "@usels/web";
// Update every 10 seconds instead of every 30
const
const timeAgo: any
timeAgo
=
import useTimeAgo
useTimeAgo
(new
any
Date
(), {
updateInterval: number
updateInterval
: 10_000 });

When showSecond: true, times under 1 minute are shown in detail (maps to date-fns includeSeconds). When showSecond: false (default), times within 45 s show "just now".

import {
import useTimeAgo
useTimeAgo
} from "@usels/web";
const
const timeAgo: any
timeAgo
=
import useTimeAgo
useTimeAgo
(new
any
Date
(
any
Date
.
any
now
() - 30_000), {
showSecond: boolean
showSecond
: true });
// → "half a minute ago"
import {
import useTimeAgo
useTimeAgo
} from "@usels/web";
const
const timeAgo: any
timeAgo
=
import useTimeAgo
useTimeAgo
(new
any
Date
("2020-01-01"), {
max: string
max
: "month",
fullDateFormatter: (d: any) => any
fullDateFormatter
: (
d: any
d
) =>
d: any
d
.
any
toLocaleDateString
(),
});
// dates older than ~11 months → formatted date string

Pass any date-fns locale to the locale option.

import {
import useTimeAgo
useTimeAgo
} from "@usels/web";
import {
import ko
ko
} from "date-fns/locale";
const
const timeAgo: any
timeAgo
=
import useTimeAgo
useTimeAgo
(new
any
Date
(), {
locale: any
locale
:
import ko
ko
});
// → "방금 전"
import {
import useTimeAgo
useTimeAgo
} from "@usels/web";
import {
import ja
ja
} from "date-fns/locale";
const
const timeAgo: any
timeAgo
=
import useTimeAgo
useTimeAgo
(new
any
Date
("2024-01-01"), {
locale: any
locale
:
import ja
ja
});
// → "約1年前"

You can also use formatTimeAgo with a locale:

import {
import formatTimeAgo
formatTimeAgo
} from "@usels/web";
import {
import fr
fr
} from "date-fns/locale";
const
const str: any
str
=
import formatTimeAgo
formatTimeAgo
(new
any
Date
("2024-01-01"), {
locale: any
locale
:
import fr
fr
}, new
any
Date
("2024-06-01"));
// → "il y a environ 5 mois"
import {
import formatTimeAgo
formatTimeAgo
} from "@usels/web";
const
const str: any
str
=
import formatTimeAgo
formatTimeAgo
(new
any
Date
("2024-01-01"), {}, new
any
Date
("2024-06-01"));
// → "about 5 months ago"
export type UseTimeAgoUnitNamesDefault = "second" | "minute" | "hour" | "day" | "week" | "month" | "year";
export interface UseTimeAgoOptions<Controls extends boolean = false> {
controls?: Controls;
updateInterval?: number;
locale?: Locale;
max?: UseTimeAgoUnitNamesDefault | number;
fullDateFormatter?: (date: Date) => string;
showSecond?: boolean;
}
export interface FormatTimeAgoOptions {
locale?: Locale;
max?: UseTimeAgoUnitNamesDefault | number;
fullDateFormatter?: (date: Date) => string;
showSecond?: boolean;
}
export declare function formatTimeAgo(from: Date, options?: FormatTimeAgoOptions, now?: Date | number): string;
export declare function useTimeAgo(time: MaybeObservable<Date | number | string>, options?: UseTimeAgoOptions<false>): ReadonlyObservable<string>;
export declare function useTimeAgo(time: MaybeObservable<Date | number | string>, options: UseTimeAgoOptions<true>): {
timeAgo$: ReadonlyObservable<string>;
} & Pausable;

View on GitHub

  • tigerwest
  • a7392ab 2026-03-06 - feat(core,browser): add sync strategy hooks (tigerwest)