Unix timestamps and time zones explained
An order placed at 1 a.m. in Dhaka shows up in yesterday’s sales report. An API returns 1790481600 and your app displays a date in January 1970. A user in another country sees a birthday one day early. Every one of these bugs comes from mixing up three things: the moment something happened, the unit it is counted in, and the time zone it is shown in. Keep them separate and date handling becomes predictable.
What Unix time is
Unix time is the number of seconds since 1970-01-01T00:00:00Z (the “epoch”), in UTC. It is a single number for a single moment, the same everywhere on Earth: 1790481600 is 04:00 UTC on 27 September 2026, which is 10:00 in Dhaka and 06:00 in Berlin at the same instant. Leap seconds are ignored — every Unix day is exactly 86,400 seconds — which keeps the arithmetic simple.
Seconds or milliseconds?
Different platforms count in different units, and the numbers look similar enough to confuse:
| Where | Returns | Example (same moment) |
|---|---|---|
JavaScript Date.now() | milliseconds (integer) | 1790481600000 |
Python time.time() | seconds (float) | 1790481600.0 |
Unix shell date +%s, most APIs, JWT exp | seconds (integer) | 1790481600 |
Java System.currentTimeMillis() | milliseconds | 1790481600000 |
Quick check: a present-day timestamp in seconds has 10 digits; in milliseconds it has 13. Mixing them produces very recognisable wrong dates:
new Date(1790481600).toISOString() // "1970-01-21T17:21:21.600Z" ✗ seconds passed as ms
new Date(1790481600 * 1000).toISOString() // "2026-09-27T04:00:00.000Z" ✓
Going the other way — milliseconds passed to a function that expects seconds — lands tens of thousands of years in the future; Python’s datetime.fromtimestamp(1790481600000) simply raises an error. If you see 1970 or an impossible year, check the unit first.
UTC, local time and ISO 8601
UTC is the reference clock with no offset and no daylight saving. Local time is UTC plus the offset of a place. When you write a date as text, use ISO 8601 with an explicit offset, so there is no guessing:
2026-09-27T04:00:00Z // UTC ("Z" = zero offset)
2026-09-27T10:00:00+06:00 // the same moment, written as Dhaka local time
2026-09-27T10:00:00 // ✗ no offset: which 10 o'clock?
The rule that prevents most bugs: store and transmit UTC (or a Unix timestamp), convert to local time only when displaying. The database, logs and APIs all speak UTC; the user interface translates it into the viewer’s zone at the last moment.
An offset is not a time zone. +06:00 describes one moment’s offset. A zone name like Asia/Dhaka carries the full history of rule changes. For future events in a local zone (a meeting at 10 a.m. next month), store the local time plus the zone name, not just a UTC instant.
Bangladesh time: UTC+6, Asia/Dhaka
Bangladesh Standard Time is UTC+6, and the IANA zone name is Asia/Dhaka. There is no daylight saving today. Bangladesh did try it once: from 19 June 2009 until the end of that year, clocks were moved to UTC+7. That one period is recorded in the time-zone database, so always convert with the zone name rather than a hard-coded +6 if you handle historical data.
Because Dhaka is six hours ahead, everything from 18:00 UTC onwards is already the next day in Bangladesh. A “today’s orders” query that uses UTC midnight will put orders made between midnight and 6 a.m. Dhaka time into the previous day.
Converting in code
// JavaScript
const ts = 1790481600; // seconds
const d = new Date(ts * 1000);
d.toISOString(); // "2026-09-27T04:00:00.000Z"
new Intl.DateTimeFormat("en-GB", {
timeZone: "Asia/Dhaka", dateStyle: "full", timeStyle: "short",
}).format(d); // "Sunday, 27 September 2026 at 10:00"
new Intl.DateTimeFormat("bn-BD", {
timeZone: "Asia/Dhaka", dateStyle: "full", timeStyle: "short",
}).format(d); // "রবিবার, ২৭ সেপ্টেম্বর, ২০২৬ এ ১০:০০ AM"
Math.floor(Date.now() / 1000); // current Unix time in seconds
# Python
from datetime import datetime, timezone
from zoneinfo import ZoneInfo # on Windows: pip install tzdata
d = datetime.fromtimestamp(1790481600, tz=timezone.utc)
d.isoformat() # '2026-09-27T04:00:00+00:00'
d.astimezone(ZoneInfo("Asia/Dhaka")).isoformat() # '2026-09-27T10:00:00+06:00'
int(datetime.now(timezone.utc).timestamp()) # current Unix time in seconds
In Python, always pass a tz. A “naive” datetime without one is silently treated as the server’s local time by .timestamp(), so the same code gives different results on your laptop and on a UTC server.
-- PostgreSQL
SELECT to_timestamp(1790481600); -- timestamptz
SELECT to_timestamp(1790481600) AT TIME ZONE 'Asia/Dhaka'; -- 2026-09-27 10:00:00
SELECT extract(epoch FROM now()); -- current Unix time
Prefer timestamptz over timestamp in PostgreSQL: it stores an absolute moment and converts on output. In MySQL, FROM_UNIXTIME() and UNIX_TIMESTAMP() use the session time zone, so set it explicitly.
The Year 2038 problem
Systems that store Unix time in a signed 32-bit integer can count up to 2,147,483,647 seconds, which is 2038-01-19T03:14:07Z (09:14:07 in Dhaka). One second later the value overflows to −2,147,483,648 and the date jumps back to 13 December 1901. Modern 64-bit systems, JavaScript and Python are safe, but watch for 32-bit INT columns holding timestamps, old embedded devices, and binary file formats with 32-bit time fields. Anything that calculates dates 15+ years ahead — loans, certificates, subscriptions — can hit it much sooner.
Common bugs and fixes
- Seconds vs milliseconds. Count the digits (10 vs 13) and name variables clearly:
createdAtMs,expiresAtSec. - JavaScript’s parsing inconsistency. A date-only ISO string is parsed as UTC, but a date-time without an offset is parsed as local time:
In a US time zone,new Date("2026-09-27") // UTC midnight new Date("2026-09-27T10:00") // 10:00 in the computer's local zone new Date("2026-09-27T10:00+06:00") // unambiguousnew Date("2026-09-27").getDate()returns26— that’s the “birthday one day early” bug. Always include an offset orZ. - Formatting with the server’s zone.
toLocaleString()without atimeZoneoption uses whatever zone the machine is in. On a cloud server that is usually UTC; passtimeZone: "Asia/Dhaka"explicitly. - Day boundaries in reports. Compute “today” in the business’s zone, convert its start and end to UTC, then query.
FAQ
Should I store dates as Unix timestamps or as database date types?
Either works if it is UTC. A native type such as PostgreSQL’s timestamptz is easier to query and read; an integer timestamp is simple to pass between systems. Use 64-bit integers if you choose numbers.
Does Unix time change with daylight saving?
No. Unix time is always UTC-based and never jumps. Only the local-time display changes when a zone switches its clocks.
Why does my Dhaka time show as GMT+6 in some places and +06:00 in others?
They are the same offset written in different styles. Intl output often uses “GMT+6”, while ISO 8601 uses +06:00.