Recipe: Date formatting per locale
Render dates that respect the user's locale without pulling in a heavy library. This recipe uses Intl.DateTimeFormat and a lightweight hook.
Ingredients
navigator.languagefor locale detectionIntl.DateTimeFormatbuilt-in API- A small custom hook for SSR safety
Steps
- Create a
useLocaleDatehook that readsnavigator.languageinside auseEffect. - Accept a
Dateor ISO string and an optional options object. - Call
new Intl.DateTimeFormat(locale, options).format(date). - Fall back to
"en-US"during SSR to avoid hydration mismatches. - Use the hook in any component that displays dates — timestamps, post dates, expiry labels.
Result
A single hook that formats dates in the user's locale with zero dependencies. German users see 31.12.2025, US users see 12/31/2025, and Japanese users see 2025/12/31 — all from the same component.