logo
Parslinks
Back to blog
Guides4 min read

Dates, time zones and non-Gregorian calendars: store, display and report without off-by-one days

An order placed at 11 p.m. shows up in tomorrow's report. The cause is almost always the same.

Alireza Tadi

Co-founder & Head of Development and Communications, Parslinks

A subscription box company in Vancouver closes the books for March, and finance can't make the numbers match the card processor. A few orders from the evening of February 28 landed in March, and several from March 31 landed in April.

The server runs on UTC, and the reporting code computed "day" from server time. Late afternoon in Vancouver is already the next day in UTC, so every order placed in the last seven or eight hours of the local day was counted as the next day.

We hit the same bug in reverse: Iran is UTC+3:30, so for us every order between midnight and 3:30 a.m. Tehran time landed on the previous day. Plus our users read dates in the Persian (Jalali) calendar. Here's the rule that fixed both.

One rule: store UTC, display local

Store every moment as an absolute timestamp. In PostgreSQL that's timestamptz, not timestamp. Never store formatted local dates as strings.

Display by converting to the user's time zone and calendar only at render time.

Compute anything that depends on a "day" or "month" (daily reports, daily spending limits, "today's orders") using local day boundaries, not UTC ones.

Storing, showing and computing dates

Display other calendars without a library

Modern browsers and Node.js understand non-Gregorian calendars. Intl.DateTimeFormat with the right locale extension renders the Persian calendar, or Hijri, Hebrew and others:

format-date.ts
const fmt = new Intl.DateTimeFormat('en-US-u-ca-persian', {
  timeZone: 'Asia/Tehran',
  year: 'numeric', month: 'long', day: 'numeric',
  hour: '2-digit', minute: '2-digit',
});
fmt.format(new Date('2026-03-20T21:30:00Z'));
// → Farvardin 1, 1405 AP, 01:00 AM (punctuation varies by runtime)
ts7 lines

Always pass timeZone explicitly. Without it, the same code prints one thing on a UTC server and another on your laptop, and the bug only shows up in production.

Monthly reports in a local calendar

For "sales in March" (or "sales in Ordibehesht"), compute the boundaries in local time first, convert them to UTC, then query. For non-Gregorian months you need a library that knows the calendar, or a calendar table in the database mapping each Gregorian day to its local date. For heavy reporting, a calendar table is faster and easier to trust.

Common traps

  • Daylight saving changes. Offsets shift, and some countries change or drop DST rules entirely (Iran abolished DST in 2022). If your OS or base image ships stale tzdata, clocks may still jump. Keep tzdata current.
  • User input. When a user picks a date in a form, they mean that day in their time zone. Account for it when converting to UTC.
  • Scheduled jobs. "Every night at 11 p.m." on a UTC server is a different local time, and it moves with DST. We cover this in scheduled jobs.

The outcome

Both teams moved their timestamp columns to timestamptz, built a calendar table for reporting and formatted every date with Intl and an explicit time zone. The next month-end close matched the processor to the cent.

Frequently asked questions

Join the discussion

No comments yet. Be the first to share your thoughts.