Fix guides · Next.js, React, Cursor, Lovable, Bolt ·
Fix Next.js hydration: Date.now() in render
Error: Hydration failed because the server rendered HTML didn't match the client.
Next.js throws a hydration mismatch when Date.now() or new Date() runs during server render. The server and browser produce different date strings, so React can't match the server HTML to the browser DOM. Move date logic into useEffect with useState so the first render shows a neutral placeholder.
Common causes – and how to tell which one you have
1. Date.now() or new Date() during server render
How to tell: The hydration overlay shows mismatched text containing a timestamp. The component calls Date.now() or new Date() during render, producing different values on server vs browser.
Fix: Move the date call into useEffect and store the result in useState: const [now, setNow] = useState(null); useEffect(() => setNow(new Date().toLocaleString()), []); Render a placeholder until now is set.
How to fix it
- Find the component from the hydration error overlay stack trace — look for Date.now(), new Date(), or toLocaleString() in render or a useState initializer.
- Make sure the component file starts with the 'use client' directive (required for useState and useEffect in the App Router). Then move the date logic into useEffect: const [now, setNow] = useState(null); useEffect(() => setNow(new Date().toLocaleString()), []);
- Render a placeholder (null or a loading state) until the effect runs and now has a value.
- Run next dev and fully reload the affected page with the browser console open — the hydration overlay appears in development.
- For production verification, run next build then next start, load the page once, and check the browser console for React minified errors #418 or #425 — these signal a hydration mismatch in production. If none appear and the page content is correct, the mismatch is likely resolved.
Using Lovable, Bolt or v0?
In Lovable, Bolt or v0: tell the AI 'Fix a Next.js hydration mismatch caused by Date.now() or new Date() during render. The component file must start with the 'use client' directive so the hooks work. Move the date into useEffect with useState(null), render a placeholder until it loads, then verify with next dev and a full browser reload.' Adding 'use client' alone does not fix the mismatch — the server still pre-renders the component once.
Using Cursor, Claude Code or Windsurf?
Ask Cursor or Claude Code to find the Date.now()/new Date() call in the stack trace, move it into a useEffect hook with useState starting at null, render a placeholder until the effect runs, then run next dev and fully reload the page with the browser console open to confirm no hydration warnings.
The “fix” to avoid
Adding 'use client' alone and stopping there — Client Components are still pre-rendered on the server once, so date calls during render still mismatch; 'use client' is required for the hooks but is not a fix by itself. Using suppressHydrationWarning broadly hides real bugs instead of fixing the cause. Rendering date-dependent content on both server and client without a placeholder also causes mismatches.
Copy-paste fix prompt for your AI coding tool
Paste this into Cursor, Claude Code, Lovable or Bolt:
My Next.js app throws 'Error: Hydration failed because the server rendered HTML didn't match the client.' and the error overlay shows a timestamp. Find where Date.now(), new Date(), or toLocaleString() is called during render or in a useState initializer. Confirm the component file starts with the 'use client' directive — useState and useEffect need it in the App Router. Move that code into useEffect that stores the result in useState starting with null: const [now, setNow] = useState(null); useEffect(() => setNow(new Date().toLocaleString()), []); Render a placeholder until now is set. Then verify with next dev and a full browser reload — the hydration warning should disappear. In production, run next build then next start, load the page, and check the console for React minified errors #418 or #425 — if none appear and the page content is correct, the mismatch is likely resolved.
Questions people ask
Does adding 'use client' fix hydration date mismatches?
No. Adding 'use client' alone does not fix the mismatch — the component is still pre-rendered on the server once, so Date.now() or new Date() called during render will differ between server and browser. But 'use client' is required for the useEffect/useState fix to work in the App Router.
Is this only a development error?
The detailed hydration mismatch overlay is dev-only, but production builds may still discard server HTML and re-render client-side on mismatch, causing flicker and slower initial loads. Fix the cause rather than ignoring it.