Server-rendered dates in the visitor's time zone, with no hydration mismatch. LocalTime renders each instant in the zone that TimeZoneProvider gets from a cookie, so the date is right in the first paint.
When to use
- Show a timestamp, such as a creation time or a renewal date, in the visitor's zone on a server-rendered page.
- Format a date in a loader or an action for that visitor, such as for a CSV export, with
readTimeZoneCookieandformatDateTime. - Use Browser Only when the text depends on other browser state, such as the clock for a "5m ago" label.
- Use Calendar when the user picks a date.
How it works
A server doesn't know the visitor's time zone, so a date formatted on the server is in the server's zone, usually UTC. The browser then renders it again in its own zone, and React reports a hydration mismatch. Time Zone fixes this in three steps:
TimeZoneScriptruns in<head>before hydration. It stores the browser's zone in themantle-tzcookie. On anngrok.comorngrok.aihost, the cookie covers every subdomain of that site.- The root loader reads the cookie from the next request with
readTimeZoneCookie. TimeZoneProvidergives the zone toLocalTimeanduseTimeZone. The server and the hydration render use it, so the HTML matches.
On a visit with no cookie, such as the first visit to a site, the server renders UTC. The hydration render uses UTC too, so React keeps the server HTML. Then the browser's zone replaces the text once. Every later request carries the cookie, and the text never changes after the first paint.
Setup
These blocks need a request, so no example on this page renders them. The examples on this page pass timeZone={undefined} and show your browser's zone after hydration.
Read the cookie in the root loader. readTimeZoneCookie returns only a valid zone name, so the other cookies in the request stay out of loader data.
Add TimeZoneScript to <head>, and wrap the app in TimeZoneProvider.
A page that a CDN caches for every visitor must not read the cookie, or one visitor's zone reaches everyone. Pass timeZone={undefined} there, or render a fixed zone with a label, such as "10:00 AM PT".
Format
LocalTime shows a medium date and a short time by default. Pass Intl.DateTimeFormat options in format to change it. The locale is en-US by default, so the server and the browser render the same text.
Format on the server
formatDateTime takes the zone as a required option, so it never falls back to the server's zone. Call it in a loader or an action with the zone from readTimeZoneCookie.
Search engines and assistants
LocalTime puts the instant in the datetime attribute in ISO 8601 form. Crawlers and answer engines read that attribute, not the visible text, so the page states the same instant to every reader in every zone.
API Reference
TimeZoneProvider
LocalTime
Renders a <time> element. Accepts every <time> prop except children and dateTime.
TimeZoneScript
useTimeZone
useTimeZone(): string returns the visitor's IANA zone, such as America/Chicago. It returns the cookie's zone on the server and in the hydration render, then the browser's zone. Outside a TimeZoneProvider, the server and the hydration render use "UTC".
readTimeZoneCookie
readTimeZoneCookie(cookieHeader: string | null | undefined): string | undefined reads the mantle-tz cookie from a raw Cookie header and returns the zone's canonical name, such as America/Chicago for america/chicago. It returns undefined when the cookie is absent or doesn't name a zone that Intl accepts. Safe to call on the server.
formatDateTime
formatDateTime(date: Date, options: FormatDateTimeOptions): string formats a date in options.timeZone, which is required. The other options are Intl.DateTimeFormat options and locale (default en-US). Safe to call on the server.