Every developer who works with dates eventually hits the same bug. A report shows yesterday's orders under today. A reminder fires an hour early in March. A user in Sydney sees a meeting at a time that never existed.
Almost all of these bugs come from one decision made early in a project: storing local time instead of UTC.
Here is the rule I follow on every project, why it works, and how to apply it in JavaScript, Python and SQL.
The rule: store in UTC, convert at the edges
Store every timestamp in UTC. Convert to a user's local time only at the very last moment, when you display it. Convert user input from local time to UTC at the very first moment, when you receive it.
Inside your system, between those two edges, there is only one clock.
Why local time breaks things
1. Daylight saving creates missing and duplicate hours.
In the US, clocks jump from 2:00 AM to 3:00 AM on the second Sunday of March. A local time of 2:30 AM that night simply does not exist. In November the clocks fall back, and 1:30 AM happens twice. UTC never jumps, so every moment has exactly one value.
2. Users and servers move.
Your server might run in Virginia today and Frankfurt next year. Your users travel. If you store local time, you also need to store whose local time it was, and most schemas forget that.
3. Sorting and comparing get unreliable.
Two local timestamps from different zones cannot be compared without extra context. Two UTC timestamps can always be compared directly.
4. Daylight saving dates differ by country.
The US and Europe switch clocks on different Sundays. For a few weeks each year the gap between New York and London is four hours instead of five. Code that hardcodes offsets breaks twice a year.
How to do it in practice
JavaScript
// Get the current moment as an ISO string in UTC const now = new Date().toISOString(); // ends with "Z", which means UTC // Store "now". Format only when you display it: const display = new Date(now).toLocaleString("en", { timeZone: "America/New_York", dateStyle: "medium", timeStyle: "short", }); The Z at the end of the string is Zulu time, another name for UTC. If you ever see an ISO string without a Z or an offset, treat it as a bug waiting to happen.
Python
from datetime import datetime, timezone from zoneinfo import ZoneInfo # Always create aware datetimes in UTC now = datetime.now(timezone.utc) # Convert only for display local = now.astimezone(ZoneInfo("Asia/Karachi")) Avoid datetime.utcnow(). It returns a naive datetime with no time zone attached, and it is deprecated in recent Python versions.
SQL (PostgreSQL)
CREATE TABLE orders ( id bigserial PRIMARY KEY, created_at timestamptz NOT NULL DEFAULT now() ); Use timestamptz, not timestamp. PostgreSQL stores timestamptz values in UTC internally and converts them using the session time zone when you read them. Setting the session or database time zone to UTC keeps what you see consistent with what is stored.
Store the user's time zone separately
UTC tells you when something happened. It does not tell you where. For features like "send this reminder at 9 AM my time every day", store the user's IANA time zone name, such as Europe/London or America/Chicago, in their profile. Never store a fixed offset like "UTC plus 1", because that offset changes with daylight saving.
A quick checklist
- Every timestamp column is UTC or
timestamptz. - Every API sends and receives ISO 8601 strings with a
Zor an explicit offset. - Servers and containers run with their system clock set to UTC.
- User time zones are stored as IANA names, not offsets.
- Conversion to local time happens only in the UI layer.
- Recurring events are calculated in the user's zone, then stored in UTC.
Tools that make debugging easier
When you are checking logs or comparing timestamps across services, it helps to keep the actual current UTC time visible instead of doing mental math. I keep a tab open with the current UTC time so I can match log entries against real time and convert to local zones in one glance.
Make UTC your default today
Time zone bugs are expensive because they hide until a daylight saving weekend or a user in a new country finds them. Pick one rule and apply it everywhere: store in UTC, convert at the edges, and save the user's zone by name. Open your schema today, check your timestamp columns, and fix the first one that is not UTC. Your future self will thank you the next time the clocks change.
