Why Developers Should Avoid Using `DateTime.now()` in Frontend Logic

Imagine this: a quiz deadline is set for February 2nd, but a clever user manually adjusts their system clock back to February 1st. If your frontend logic relies on DateTime.now()
, they might just bypass the deadline entirely.
This isn't just a hypothetical issue—here are real-world scenarios where relying on client-side time can break your app:
DateTime.now()
?The method DateTime.now()
retrieves the current time from the user’s local system clock. This makes it very easy to manipulate and introduces several risks.
DateTime.now()
on the FrontendFrontend logic should never be the source of truth for time. Instead, shift all time-sensitive validation to your backend.
Let’s say a user submits a form for a time-limited event:
// Frontend: send request without relying on local clock await fetch('/api/submit-quiz', { method: 'POST', body: JSON.stringify({ answers }), });
// Backend: validate using server time const now = new Date(); const deadline = new Date('2025-02-02T23:59:59Z'); if (now > deadline) { return res.status(400).json({ error: 'Deadline passed' }); } // Proceed with saving the quiz submission
Yes — but only for display purposes like:
Always pair frontend displays with backend enforcement to avoid abuse.
Relying on DateTime.now()
in frontend logic is like trusting the fox to guard the henhouse. Users can and will manipulate their system time — and when that happens, your app could be at risk.
✅ Always validate time-sensitive logic on the backend. ✅ Trust server time, not client clocks. ✅ Use client-side time only for visual convenience.
Your users (and your backend) will thank you. 🚀