Skip to main content

Function: useNow()

useNow(options?): number

Defined in: use-now.ts:243

A custom React hook that returns the current time in milliseconds and re-renders the component at a fixed interval.

Every component that uses the same intervalMs shares one timer, so hundreds of "5 minutes ago" labels cost a single setInterval. The timer starts with the first subscriber and stops with the last. The hook is built on useSyncExternalStore, so every component in a render sees the same value and updates never tear under concurrent rendering.

By default ticks stop while the tab is hidden, and the value refreshes as soon as the tab is visible again. Pass enabled: false to stop updating, for example to pause a stopwatch: the hook keeps returning the last value it returned while enabled.

The result is a plain number, so you can derive anything from it: wrap it in new Date(now), add a server clock offset, or compute a remaining time. To limit re-renders, pick an interval as coarse as your UI needs (one minute for relative labels, one second for countdowns). The value can be up to one interval older than a Date.now() you read yourself, so clamp differences that must not be negative.

On the server, and while hydrating, the hook returns serverNow when given, and the current time otherwise. Time-dependent text then differs from the server's HTML unless you pass the same serverNow on both sides (for example a timestamp from props) or use suppressHydrationWarning.

Parameters​

options?​

UseNowOptions

Optional interval, pause and server-time settings.

Returns​

number

The current time in milliseconds since the epoch, as Date.now() returns.

Examples​

const RelativeTime = ({ date }: { date: number }) => {
const now = useNow({ intervalMs: 60_000 });
const minutes = Math.max(0, Math.round((now - date) / 60_000));
return <span>{minutes} min ago</span>;
};
// Countdown that stops once the deadline passes
const now = useNow({ enabled: now < deadline });
// Same markup on the server and in the browser
const now = useNow({ serverNow: props.renderedAt });

For a live, editable example, see the useNow docs page.