Next.js 'Hydration failed because the initial UI does not match' from a date formatter
The page looked completely normal. That's what made this hydration error so annoying to track down. Next.js 14.2.5, app router, and a dashboard card showing "3 hours ago" next to each order. Every page load put this in the browser console:
Uncaught Error: Hydration failed because the initial UI does not match what was
rendered on the server.
at throwOnHydrationMismatch (react-dom.development.js:13826:13)
at tryToClaimNextHydratableInstance (react-dom.development.js:13869:15)
at updateHostText (react-dom.development.js:14241:7)
There was no visible broken UI, React just silently re-rendered the tree on the client. So this was a correctness problem hiding in plain sight.
## How I found the actual difference
The generic error doesn't tell you which node differed. Compare the server HTML to the client DOM directly:
bash
curl -s http://localhost:3000/dashboard > server.html
Then, in the browser devtools, copy the outerHTML of the same element and diff the two. The difference was one text node:
diff
- <span>3 hours ago</span> <!-- server -->
+ <span>4 hours ago</span> <!-- client -->
The server rendered at UTC, the browser rendered in the user's local timezone, and our formatter included the current time in the calculation. Any gap between server render and hydration produced a different number of hours. Around a boundary it flipped from 3 to 4, which is exactly when React complained.
## The root cause was "now"
Anything that reads the current time, Date.now(), Math.random(), window, or localStorage during render is a hydration hazard, because the server and the client are two different environments at two different moments. Our timeAgo() helper called new Date() internally, so every call was a fresh mismatch risk.
The fix is to make the server output deterministic. Pass a reference time in as a prop:
tsx
export function TimeAgo({ iso, now }: { iso: string; now: number }) {
const diff = now - new Date(iso).getTime();
const hours = Math.floor(diff / 3_600_000);
return <span>{hours} hours ago</span>;
}
And compute now once, on the server, per request:
tsx
// app/dashboard/page.tsx
export default async function Page() {
const now = Date.now();
const orders = await getOrders();
return <OrderList orders={orders} now={now} />;
}
Now server and client both render with the same number, because it travels in the HTML as a prop. A useEffect can update the label after hydration if you want it to stay fresh.
## The escape hatch, and when I used it
For a label where a one-render flicker is harmless, suppressHydrationWarning is a legitimate tool:
tsx
<span suppressHydrationWarning>{timeAgo(order.createdAt)}</span>
It only suppresses the warning for that element's text content, not the whole subtree. I used it on a clock in the header and nowhere else. Overusing it hides real mismatches.
## The other common source: browser-only values
A useState(() => localStorage.getItem("theme")) initializer also causes this, because localStorage doesn't exist on the server. The pattern is to initialize with a fixed default, then read the stored value in useEffect:
tsx
const [theme, setTheme] = useState("light");
useEffect(() => setTheme(localStorage.getItem("theme") ?? "light"), []);
Anyway, the error text is misleading — nothing "failed" visibly. It means React threw away the server markup and rebuilt the client tree, which costs performance and can wipe focus or scroll state. Worth fixing properly rather than suppressing globally.