Unix Time, ISO 8601 and Time Zones: The Rules Behind Every Off-by-One-Day Bug
Timestamps look simple — a number, or a string with a T in the middle — and they produce a disproportionate share of production bugs: a report that is a day off, a scheduled job that runs twice, an expiry that fires an hour early each spring. Almost all of them come from a small set of confusions, and this guide names each one so you can recognise it before it ships.
The short version
- A Unix timestamp is an instant: seconds since 1970-01-01 00:00:00 UTC. It has no time zone. Only a wall-clock time has a zone.
- Seconds vs milliseconds is a factor-of-1000 bug that reads as "the year 56,000" or "January 1970". Check the digit count: 10 is seconds, 13 is milliseconds.
- A zone (
Asia/Dubai) is a set of rules; an offset (+04:00) is one value from those rules at one moment. Store the zone name, never just the offset. - Store instants in UTC. Convert to local time only at display. For future appointments, also store the zone name.
- ISO 8601 with an explicit
Zor offset is the only string form worth exchanging between systems.
Instants and wall-clock times are different things
The single most useful idea in this whole topic is the distinction between an instant — a point on the universal timeline, the same everywhere on Earth — and a wall-clock time — what a clock on a particular wall shows. 1727438400 is an instant. "12:00 on 27 September 2024" is a wall-clock time and is meaningless until you say whose wall: it is a different instant in Dubai, London and São Paulo.
Unix time is a count of seconds since the epoch, 00:00:00 UTC on 1 January 1970, and it is an instant. That is why it is the right format for systems to exchange and store: no zone, no ambiguity, no daylight-saving rules, and arithmetic is plain subtraction. Every bug in this guide is, at bottom, a wall-clock time being treated as an instant or an instant being displayed as if it were a wall-clock time in the wrong zone.
Seconds, milliseconds and the year 56,000
Unix tradition, most databases and JWT claims count seconds. JavaScript's Date.now(), Java's System.currentTimeMillis(), Go's UnixMilli() and most JSON APIs count milliseconds. Some tracing systems use microseconds or nanoseconds. Mix them and you get a date tens of thousands of years away (milliseconds read as seconds) or a date in January 1970 (seconds read as milliseconds).
The tell is the digit count. Current time in seconds is a 10-digit number; in milliseconds, 13; microseconds, 16; nanoseconds, 19. A JWT whose exp has 13 digits was written with Date.now() instead of Math.floor(Date.now() / 1000), and every validator will treat it as never expiring. Layerbit's timestamp converter reads the unit off the magnitude and says which it assumed, and its JWT inspector warns when a claim looks like milliseconds.
ISO 8601: which form you actually want
ISO 8601 is a family of formats, and only one member of it is safe for interchange: the full date-time with an explicit zone designator, 2026-09-27T12:00:00Z or 2026-09-27T16:00:00+04:00. The Z means UTC; the offset means "this wall-clock time, at this offset from UTC". Either way the instant is unambiguous. RFC 3339 is the profile of ISO 8601 that requires this, and it is what "ISO date" should mean in an API contract.
The forms without a zone are where trouble starts. 2026-09-27T12:00:00 is a wall-clock time with no wall; each parser picks one. JavaScript's Date parses that string as local time, but parses the date-only 2026-09-27 as UTC midnight — so in New York the second one displays as the evening of the 26th. That inconsistency, inherited from an old spec quirk, is the direct cause of a great many "the date is one day off" reports, and the fix is to never emit a date-time string without a Z or offset.
A zone is not an offset
+04:00 is a number. Asia/Dubai is a name for a set of rules, maintained in the IANA time zone database, that say what the offset is at every moment in history and (as far as legislation allows) the future. For Dubai the rules are simple: +04:00 always. For Europe/London the offset is +00:00 in winter and +01:00 in summer, and the dates of the switch have changed several times over the decades. For America/Sao_Paulo daylight saving was abolished in 2019, so an offset stored in 2018 for a 2020 appointment was wrong by the time it arrived.
This is why storing an offset with a future wall-clock time is a bug waiting to happen. If a user books "9 am on 15 March" in London, storing 09:00+00:00 gives the wrong instant if the clocks have changed by then. Store the wall-clock time plus the zone name, and compute the instant when it is needed using the rules current at that time. Browsers ship the IANA database through Intl.DateTimeFormat, which is how a purely client-side converter can show the correct historical offset for any date.
Convert any timestamp in any zone, locally
Layerbit's Unix Timestamp Converter detects the unit, shows the instant as ISO 8601, RFC 2822 and in any IANA zone with the offset in force at that moment, flags 32-bit overflow, and does calendar-correct arithmetic — all in the browser.
Daylight saving: the hour that happens twice
Twice a year, in zones that observe daylight saving, wall-clock time misbehaves. In spring the clocks jump forward and a whole hour of wall-clock times — 02:00 to 02:59 in most of Europe and North America — never happens. In autumn the clocks fall back and an hour happens twice; 01:30 refers to two different instants. Any code that schedules by wall-clock time has to decide what to do with these, and most code does not decide, it just breaks.
- A cron job at
30 2 * * *on a server in a DST zone does not run on the spring-forward night and runs twice on the fall-back night. Run schedulers in UTC. - "Add one day" to an instant by adding 86,400 seconds gives a wall-clock time an hour off across a transition. Calendar arithmetic must be done in the zone, not on the epoch number.
- "Add 24 hours" and "add one day" are different operations on a DST day, and a function that does not distinguish them is wrong on two days a year.
What to store
| You are recording… | Store | Why |
|---|---|---|
| When something happened (a log, an order, a login) | The instant, in UTC: timestamptz, or epoch seconds/milliseconds as an integer | It is a point in time; the zone of the observer is irrelevant to the fact. |
| When something will happen at a place (an appointment, a store opening) | Wall-clock time plus the IANA zone name | The instant may move if the zone's rules change before the date arrives. |
| A date with no time (a birthday, a due date) | A plain date, DATE or YYYY-MM-DD, never a midnight timestamp | Midnight is a different instant in every zone; a date-as-timestamp shifts by a day at the boundaries. |
| A duration | Seconds, or an ISO 8601 duration such as PT1H30M | A start and end pair can drift across DST; a duration cannot. |
Two database notes. MySQL's TIMESTAMP column silently converts to and from the session time zone, so the same row reads differently from two connections with different settings; DATETIME stores what you give it with no conversion. PostgreSQL's timestamptz stores UTC and converts on the way out, which is the behaviour you want, while plain timestamp stores a naked wall-clock time and is almost never what you want.
Converting Unix time in code, SQL and Excel
Every language and database can do the conversion; the differences are in which unit it expects and which zone it silently applies. The examples below all use the same instant, 1727438400, which is 12:00 UTC on Friday 27 September 2024, or 16:00 in Dubai. Each one was run to check the output shown.
JavaScript
Math.floor(Date.now() / 1000) // now, in seconds (Date.now() is milliseconds)
new Date(1727438400 * 1000).toISOString() // "2024-09-27T12:00:00.000Z"
Math.floor(Date.parse("2024-09-27T12:00:00Z") / 1000) // 1727438400
new Date(1727438400 * 1000).toLocaleString("en-GB", { timeZone: "Asia/Dubai" })
// "27/09/2024, 16:00:00"
The multiply and divide by 1000 is the whole story in JavaScript: Date works in milliseconds and almost everything it talks to works in seconds.
Python
import time
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
int(time.time()) # now, in seconds
datetime.fromtimestamp(1727438400, tz=timezone.utc) # 2024-09-27 12:00:00+00:00
datetime.fromtimestamp(1727438400, tz=ZoneInfo("Asia/Dubai")) # 2024-09-27 16:00:00+04:00
int(datetime(2024, 9, 27, 12, tzinfo=timezone.utc).timestamp()) # 1727438400
Always pass tz. Without it, fromtimestamp returns a naive time in the server's local zone, and the older utcfromtimestamp returns a naive UTC time that is deprecated since Python 3.12 because it is so easily mistaken for local time.
PostgreSQL
SELECT to_timestamp(1727438400); -- 2024-09-27 12:00:00+00
SELECT to_timestamp(1727438400) AT TIME ZONE 'Asia/Dubai'; -- 2024-09-27 16:00:00
SELECT extract(epoch FROM now())::bigint; -- now, in seconds
SELECT to_timestamp(1727438400123 / 1000.0); -- from milliseconds
to_timestamp returns a timestamptz, which is displayed in the session's TimeZone setting; the first line shows +00 only because the session is in UTC.
MySQL
SELECT FROM_UNIXTIME(1727438400); -- in the session time zone
SELECT UNIX_TIMESTAMP(); -- now, in seconds
SELECT CONVERT_TZ(FROM_UNIXTIME(1727438400), @@session.time_zone, '+04:00');
Both FROM_UNIXTIME and UNIX_TIMESTAMP('2024-09-27 12:00:00') use the session time zone, so the same query gives different answers on two connections. Run SET time_zone = '+00:00' first if you want UTC. Named zones such as 'Asia/Dubai' in CONVERT_TZ only work once the time zone tables have been loaded on the server.
SQL Server
SELECT DATEADD(SECOND, 1727438400, '1970-01-01'); -- 2024-09-27 12:00:00.000
SELECT DATEDIFF_BIG(SECOND, '1970-01-01', SYSUTCDATETIME()); -- now, in seconds
-- milliseconds: DATEADD takes an int, so add whole seconds first
SELECT DATEADD(MILLISECOND, 1727438400123 % 1000,
DATEADD(SECOND, 1727438400123 / 1000, CAST('1970-01-01' AS datetime2(3))));
-- Dubai time (SQL Server 2016 and later)
SELECT CAST(DATEADD(SECOND, 1727438400, '1970-01-01') AS datetime2)
AT TIME ZONE 'UTC' AT TIME ZONE 'Arabian Standard Time';
SQL Server has no built-in epoch function, so DATEADD from 1970 is the standard idiom. Its number argument is a 32-bit int, which is why a 13-digit millisecond value fails with an arithmetic overflow and has to be split as shown. AT TIME ZONE uses Windows zone names, so Dubai is 'Arabian Standard Time', not 'Asia/Dubai'.
Oracle
SELECT DATE '1970-01-01' + 1727438400 / 86400 FROM dual; -- 2024-09-27 12:00:00, in UTC
SELECT (TIMESTAMP '1970-01-01 00:00:00 UTC'
+ NUMTODSINTERVAL(1727438400, 'SECOND')) AT TIME ZONE 'Asia/Dubai' FROM dual;
-- now, in seconds
SELECT ROUND((CAST(SYS_EXTRACT_UTC(SYSTIMESTAMP) AS DATE) - DATE '1970-01-01') * 86400)
FROM dual;
Oracle date arithmetic is in days, hence the division and multiplication by 86,400. A plain DATE carries no zone, so remember that the first result is UTC; use SYS_EXTRACT_UTC(SYSTIMESTAMP) rather than SYSDATE for "now", because SYSDATE is the database server's local time.
C# and Java
// C# (.NET)
DateTimeOffset.FromUnixTimeSeconds(1727438400) // 2024-09-27 12:00:00 +00:00
DateTimeOffset.UtcNow.ToUnixTimeSeconds() // now, in seconds
DateTimeOffset.FromUnixTimeMilliseconds(1727438400123)
// Java (java.time)
Instant.ofEpochSecond(1727438400) // 2024-09-27T12:00:00Z
Instant.now().getEpochSecond() // now, in seconds
Instant.ofEpochSecond(1727438400).atZone(ZoneId.of("Asia/Dubai"))
// 2024-09-27T16:00+04:00[Asia/Dubai]
In .NET prefer DateTimeOffset over DateTime, whose Kind property is easy to lose; in Java avoid the legacy java.util.Date and SimpleDateFormat in new code.
Command line
date +%s # now, in seconds
date -u -d @1727438400 # Fri Sep 27 12:00:00 UTC 2024 (Linux, GNU date)
TZ=Asia/Dubai date -d @1727438400 # Fri Sep 27 16:00:00 +04 2024
date -u -r 1727438400 # the same on macOS and BSD
Excel and Google Sheets
=A2/86400 + DATE(1970,1,1) seconds in A2, result in UTC
=A2/86400000 + DATE(1970,1,1) milliseconds in A2
=A2/86400 + DATE(1970,1,1) + 4/24 shifted to UTC+4 (Dubai)
=(B2 - DATE(1970,1,1)) * 86400 a date-time in B2 back to seconds
Format the result cell as a date and time, or it will show a serial number such as 45562.5. Spreadsheets have no time zones, so the fixed +4/24 shift is only correct for zones without daylight saving; for London or New York the offset depends on the date.
The edges: 2038, leap seconds, negative time
A signed 32-bit integer of seconds overflows at 03:14:07 UTC on 19 January 2038. Modern systems use 64 bits, but embedded firmware, older file formats, some database schemas and a surprising amount of C code with int where time_t was meant do not. If a system will still be running in 2038 — and industrial and medical equipment often is — check the width now. Leap seconds are simpler than they sound: Unix time ignores them by definition, pretending every day is exactly 86,400 seconds, which keeps arithmetic simple at the cost of drifting from atomic time by 27 seconds so far. Negative timestamps are legal and mean before 1970; they work almost everywhere except MySQL's TIMESTAMP type, which cannot represent them.
One rule covers most of it
Represent time as an instant in UTC everywhere inside the system, attach a zone name only where a future wall-clock time is the actual requirement, and convert to local time at the last possible moment for display. Systems built on that rule still have to handle DST edge cases and unit mismatches at their boundaries, but they do not have the pervasive, hard-to-reproduce off-by-an-hour and off-by-a-day bugs that come from letting wall-clock times masquerade as instants deep in the data model.
Related guides and tools
Frequently asked questions
No. It counts seconds since 1970-01-01 00:00:00 UTC and identifies a single instant that is the same everywhere. A time zone only enters the picture when you convert that instant into a wall-clock date and time for display, and that conversion is where the zone must be supplied explicitly.
Count the digits. Current Unix time is a 10-digit number in seconds and a 13-digit number in milliseconds; microseconds have 16 and nanoseconds 19. A value that converts to the year 56,000 was milliseconds read as seconds; one that converts to January 1970 was seconds read as milliseconds.
Store instants — things that have happened — in UTC, and convert to local time only when displaying. Store future appointments as a wall-clock time together with the IANA zone name, because the zone's offset rules may change before the date arrives. Never store just an offset like +04:00 in place of a zone name.
A historical quirk of the ECMAScript specification: date-only ISO strings are parsed as UTC midnight, while date-time strings without a zone designator are parsed as local time. The inconsistency causes many off-by-one-day bugs. Avoid it by always including a Z or an explicit offset in any date-time string you exchange, and by treating date-only values as plain dates rather than timestamps.
Divide the seconds by 86,400 (the number of seconds in a day) and add the spreadsheet's date for 1 January 1970: =A2/86400 + DATE(1970,1,1), then format the cell as a date and time. Use 86,400,000 instead for millisecond timestamps. The result is in UTC; add the offset as a fraction of a day, such as +4/24 for Dubai, to show local time in a zone without daylight saving.
SQL Server has no epoch function, so add the seconds to 1 January 1970: SELECT DATEADD(SECOND, 1727438400, '1970-01-01'). The result is UTC. DATEADD only accepts a 32-bit integer, so split millisecond values into whole seconds plus the remaining milliseconds, and use DATEDIFF_BIG(SECOND, '1970-01-01', SYSUTCDATETIME()) to get the current timestamp.
A signed 32-bit count of seconds overflows on 19 January 2038 at 03:14:07 UTC, wrapping to a date in 1901. Current operating systems use 64-bit time, but embedded devices, older file formats and some database columns do not, and equipment installed today may still be running then. Check any long-lived system that stores time in 32 bits.