· 8 min read
Date and Time Zone Pitfalls: Store in UTC, Show the Right Time
Dates and times are among the most underestimated data types in software. Is 2026-10-01 a day or a moment? Which time zone is the server in? Where is the user? When the code does not answer these questions explicitly, the bug often shows up only in some countries, only near midnight, or only on New Year's Day. This post is about date and time zone pitfalls in four of my projects and how I handled each one: a day shift in JavaScript, a city's wrong clock, a cinema schedule pinned to Istanbul time, storing UTC in C#, and times stored as text in Java.
First, two concepts: a moment and a day
The distinction behind all of them:
- An instant: a point that is the same everywhere. The show starts at 18:00 UTC on 14 October 2026. That is 21:00 in Istanbul and 19:00 in London, but the same instant.
- A calendar date or wall-clock time: tied to a place.
Thursday 1 October,due on 20 November. Without a time zone it cannot be turned into an instant.
Most of the bugs come from mistaking one for the other.
1. Weather: Thursday becomes Wednesday in New York
The 5-day forecast in my weather app gives a date such as "2026-10-01" for each day. The obvious way to print the weekday:
new Intl.DateTimeFormat('tr', { weekday: 'long' }).format(new Date('2026-10-01'));
A JavaScript quirk kicks in here: a date-only string like "2026-10-01" is parsed as midnight UTC, while formatting happens in the user's time zone. West of UTC, midnight UTC is the evening of the previous day in local time:
| User's time zone | new Date('2026-10-01') | Read at noon UTC |
|---|---|---|
| New York | Wednesday | Thursday |
| Istanbul | Thursday | Thursday |
| Tokyo | Thursday | Thursday |
And another quirk: add a time to the same string without a trailing Z, as in "2026-10-01T00:00", and it is parsed as local time instead. One character changes which time zone the string is read in.
Here the date is not an instant but a calendar day. The fix is to treat it as a day, not a moment: read it at midday in UTC and format it in UTC. The day stays the same wherever the user is:
// The date comes as "2026-09-30"; read at noon UTC, the time zone cannot shift the day
return new Intl.DateTimeFormat(settings.lang, { weekday: 'long', timeZone: 'UTC' }).format(new Date(`${date}T12:00:00Z`));
2. Weather: the city's clock stuck at 22:00
The same app shows the local time of the selected city. The first version took it from the weather data's timestamp. Open-Meteo gives current conditions in 15-minute slots, and the timestamp is the start of the slot: at 22:13 it showed 22:00. The timestamp says which moment the data belongs to; it is not the current time.
The right way is to take the current instant and format it in the city's time zone. The API returns the city's IANA time zone, such as Europe/Istanbul or Asia/Kolkata; if the browser does not know that name, the UTC offset in the response is the fallback:
export function localTime(forecast, now = Date.now()) {
if (forecast?.timeZone) {
try {
return new Intl.DateTimeFormat('en-GB', {
timeZone: forecast.timeZone, hour: '2-digit', minute: '2-digit', hourCycle: 'h23',
}).format(now);
} catch {
// A time zone the browser doesn't know: fall back below
}
}
if (typeof forecast?.utcOffset !== 'number') return null;
return new Date(now + forecast.utcOffset * 1000).toISOString().slice(11, 16);
}
A time zone name is more reliable than a fixed offset: in a city with daylight saving time the offset changes twice a year, the name does not. The test includes India, with its half-hour offset:
assert.equal(localTime({ timeZone: 'Europe/Istanbul', utcOffset: 10800 }, now), '22:13');
assert.equal(localTime({ timeZone: 'Asia/Tokyo', utcOffset: 32400 }, now), '04:13');
assert.equal(localTime({ timeZone: 'Asia/Kolkata', utcOffset: 19800 }, now), '00:43');
3. Seans: the cinema is in Istanbul, wherever the visitor is
In Seans, my cinema booking site, showtimes are stored in UTC. But the cinema is in Istanbul: someone browsing from Berlin should see that the film starts at 21:00 Istanbul time, not a value converted to their computer's clock. So every date on screen is written in Istanbul time, whatever the visitor's computer says:
// Dates and money, always in Istanbul time and Turkish lira, whatever the visitor's computer says.
const TZ = 'Europe/Istanbul';
export const formatTime = (date, lang) =>
new Intl.DateTimeFormat(locale(lang), { timeZone: TZ, hour: '2-digit', minute: '2-digit' }).format(new Date(date));
Grouping showtimes by day follows the same rule. A show at 22:30 UTC is 01:30 the next day in Istanbul and must appear under the next day in the programme. The day key is built in Istanbul time too:
// "2026-10-14" for a date in Istanbul, used to group showtimes by day.
export const dayKey = (date) =>
new Intl.DateTimeFormat('en-CA', { timeZone: TZ, year: 'numeric', month: '2-digit', day: '2-digit' }).format(new Date(date));
en-CA was picked because it formats a date directly as YYYY-MM-DD.
When adding a show in the admin panel, the form sends the time with +03:00, and the server accepts only times with an explicit offset. Turkey has had no daylight saving time since 2016, so +03:00 is fixed and a day in Istanbul always starts at 21:00 UTC:
startsAt: z.iso.datetime({ offset: true }), // the page sends Istanbul time with "+03:00"
4. Tamirhane: store in UTC, think in Turkey
In Tamirhane, my repair shop app, times are DateTimeOffset values and the app works in Turkey time, +03:00. The current time comes from one place:
public static class TurkeyTime
{
public static readonly TimeSpan Offset = TimeSpan.FromHours(3);
public static DateTimeOffset Now(TimeProvider clock) => clock.GetUtcNow().ToOffset(Offset);
}
The clock is not DateTime.Now but an injected TimeProvider. Tests can set it to any moment, so rules such as the due date cannot be before today are tested against a fixed date.
PostgreSQL brought a surprise: Npgsql refuses to write a DateTimeOffset with a +03:00 offset into a timestamptz column and accepts only UTC. Everything is stored in UTC in the database anyway; the offset just has to be removed explicitly on the way in. Instead of doing that everywhere, I defined it once for every date field:
// PostgreSQL timestamptz accepts only UTC; Turkey time (+03:00) is converted to UTC when written.
builder.Properties<DateTimeOffset>().HaveConversion<UtcConverter>();
builder.Properties<DateTimeOffset?>().HaveConversion<UtcConverter>();
In two places the question what day is it in Turkey? matters:
- Work order numbers start with the year:
2027-0001. A work order opened at 01:00 Turkey time on 1 January is still 22:00 on 31 December in UTC. Taking the year from UTC would give the first order of the year the previous year's number. The year comes from Turkey time. - The due date is a calendar day, not an instant. So it is stored as a
DateOnly, andtodayis Turkey's today:
var promised = order.Value.Promise(c.PromisedDate, DateOnly.FromDateTime(now.DateTime));
5. Bank Management: times stored as text
My banking app in Java uses SQLite. SQLite has no separate date type; times are stored as text and queries compare them as text, for example to find accounts that have matured or transactions after today. ISO-formatted strings also sort correctly in alphabetical order, so this usually works. On one condition: they must always be written in the same format.
Java's LocalDateTime.toString() breaks that condition: when the seconds are zero, it leaves them out.
LocalDateTime.of(2026, 10, 9, 9, 0, 0).toString(); // "2026-10-09T09:00"
LocalDateTime.of(2026, 10, 9, 9, 0, 5).toString(); // "2026-10-09T09:00:05"
The same instant could be written two ways: "2026-10-09T09:00" and "2026-10-09T09:00:00". As text they are not equal; the shorter one sorts before the longer one. So a query with >= '2026-10-09T09:00:00' could miss a record stored as "...T09:00" for the very same instant.
The fix is to write every time through one function, always with seconds, and to round the current time to whole seconds:
public static String ts(LocalDateTime time) {
return time.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
return LocalDateTime.now(clock).truncatedTo(ChronoUnit.SECONDS);
The rounding is for the same reason: fractions of a second are sometimes written and sometimes not, which again produces strings of different lengths.
What I took away
- First ask: is this an instant or a day? An instant is stored in UTC and converted to a time zone for display. A day, such as a birthday, a due date or a forecast day, is kept as a date without a time zone and never turned into an instant.
- The display time zone is a decision. The city's time for weather, the cinema's time for a cinema, the shop's time for a shop. The user's computer clock is often not the right answer.
Todayand this year are computed in a time zone. UTC's today is not Istanbul's today; the difference shows at midnight and on New Year's Day.- The current time is injected. A clock from
TimeProviderorClockcan be set to any moment in a test. - Times stored as text are written in one format. Otherwise the same instant becomes two different strings and comparisons go quietly wrong.