Skip to content
Derar Sattouf

ISO 8601 to Unix Timestamp in JavaScript, Python, SQL and the Shell

Backend & databases

ISO 8601 to Unix Timestamp in JavaScript, Python, SQL and the Shell

An ISO 8601 string such as 2026-11-23T14:30:00Z and a Unix timestamp such as 1795444200 describe the same instant in two ways. The ISO form is readable and carries its time zone offset. The Unix form is the number of seconds since 1 January 1970 at midnight UTC, which makes it easy to store, sort and subtract.

To convert one value without writing code, paste it into the Unix Timestamp Converter. It detects seconds or milliseconds, shows the ISO 8601 form in UTC and in any time zone, and works both ways in the browser.

For code, here is the short version. Every example below converts 2026-11-23T14:30:00Z and gets 1795444200:

WhereISO 8601 to Unix secondsUnix seconds to ISO 8601
JavaScriptMath.floor(Date.parse(iso) / 1000)new Date(ts * 1000).toISOString()
Python 3.11+int(datetime.fromisoformat(iso).timestamp())datetime.fromtimestamp(ts, tz=timezone.utc).isoformat()
PostgreSQLEXTRACT(EPOCH FROM iso::timestamptz)::bigintto_timestamp(ts)
MySQLUNIX_TIMESTAMP('2026-11-23 14:30:00') with a UTC sessionFROM_UNIXTIME(ts)
SQLiteunixepoch(iso)datetime(ts, 'unixepoch')
GNU date (Linux)date -d "$iso" +%sdate -u -d @"$ts" +%Y-%m-%dT%H:%M:%SZ
BSD date (macOS)date -j -u -f "%Y-%m-%dT%H:%M:%SZ" "$iso" +%sdate -u -r "$ts" +%Y-%m-%dT%H:%M:%SZ

The rest of this post explains each one and the details that make results come out an hour, a day or a factor of 1,000 wrong.

The two formats in one minute

ISO 8601 writes a date and time from the largest unit to the smallest, with an offset at the end:

  • 2026-11-23T14:30:00Z: the Z means UTC (offset zero).
  • 2026-11-23T16:30:00+02:00: the same instant, written in a zone two hours ahead of UTC.
  • 2026-11-23T14:30:00.750Z: with milliseconds.
  • 2026-11-23T14:30:00: no offset. This is a local time, and on its own it does not say which instant it is.

What APIs and databases call "ISO 8601" is nearly always the stricter profile defined in RFC 3339: a full date, a T, a full time and an offset. ISO 8601 also allows forms such as week dates (2026-W48-1) that most parsers reject.

Unix time counts seconds from 1970-01-01T00:00:00Z, the Unix epoch. It has no time zone, because it counts from a fixed instant. Two details matter in practice:

  • Seconds or milliseconds. Unix tools, most databases and Python use seconds. JavaScript, Java and many JSON APIs use milliseconds. A current timestamp has 10 digits in seconds and 13 in milliseconds.
  • No leap seconds. Unix time treats every day as exactly 86,400 seconds. A leap second such as 23:59:60 cannot be represented, and most parsers reject it.

JavaScript

Date.parse returns milliseconds since the epoch, or NaN if it cannot read the string:

const iso = "2026-11-23T14:30:00Z";

const ms = Date.parse(iso);            // 1795444200000
const seconds = Math.floor(ms / 1000); // 1795444200

if (Number.isNaN(ms)) throw new Error(`Not a valid date: ${iso}`);

And back:

new Date(1795444200 * 1000).toISOString(); // "2026-11-23T14:30:00.000Z"

What to watch for:

  • No offset means local time, unless there is no time. Date.parse("2026-11-23") is read as midnight UTC, but Date.parse("2026-11-23T14:30:00") is read as 14:30 in the time zone of the machine running the code. The same line of code gives different timestamps on a laptop in Beirut and a server in UTC. Always send and store ISO strings with Z or an offset.
  • Only the ISO format is guaranteed. Date.parse must accept the ISO format. Anything else, such as "2026-11-23 14:30:00" with a space, or "23/11/2026", is left to each engine, and engines disagree. Parse those formats yourself or with a library.
  • toISOString() always returns UTC with milliseconds and a Z. To show a time in a specific zone, use Intl.DateTimeFormat with a timeZone option.
  • Math.floor, not Math.trunc, if timestamps before 1970 are possible. For -1.5 seconds, Math.floor gives -2 and Math.trunc gives -1, and -2 is the correct whole second.

Temporal, the newer date API, handles this more strictly: Temporal.Instant.from(iso).epochMilliseconds throws on a string without an offset instead of guessing. Support is still uneven (Node.js 22 does not have it), so check your targets before relying on it.

Python

Since Python 3.11, datetime.fromisoformat reads almost any ISO 8601 string, including the trailing Z:

from datetime import datetime, timezone

iso = "2026-11-23T14:30:00Z"
dt = datetime.fromisoformat(iso)   # 2026-11-23 14:30:00+00:00
seconds = int(dt.timestamp())      # 1795444200

And back:

datetime.fromtimestamp(1795444200, tz=timezone.utc).isoformat()
# '2026-11-23T14:30:00+00:00'

What to watch for:

  • Python 3.10 and older reject Z with "Invalid isoformat string". macOS still ships an older Python 3 in some setups. Replace it first: datetime.fromisoformat(iso.replace("Z", "+00:00")).
  • Naive datetimes are local time. A string without an offset gives a datetime with tzinfo=None, and .timestamp() then assumes the machine's local zone. If the value is known to be UTC, attach it: dt.replace(tzinfo=timezone.utc).
  • utcfromtimestamp is deprecated since Python 3.12, because it returns a naive datetime that is easy to misread. Use fromtimestamp(ts, tz=timezone.utc).
  • .timestamp() returns a float. int() drops the fractional part, which is right for times after 1970.

SQL

PostgreSQL

Cast the string to timestamptz and extract the epoch:

SELECT EXTRACT(EPOCH FROM '2026-11-23T14:30:00Z'::timestamptz)::bigint;
-- 1795444200

SELECT to_timestamp(1795444200);
-- 2026-11-23 14:30:00+00 (displayed in the session's TimeZone setting)

EXTRACT returns a numeric with a fractional part, and casting numeric to bigint rounds. If values can have fractions of a second and you need the whole second they fall in, use floor(EXTRACT(EPOCH FROM ...))::bigint.

Store instants in timestamptz, not timestamp. A timestamptz column stores an exact instant and converts on display. A plain timestamp column stores a wall-clock time with no zone, which brings back the "which instant is this?" problem.

MySQL

MySQL reads date strings in the session time zone and does not accept the trailing Z, so set the session to UTC and pass the date and time without it:

SET time_zone = '+00:00';

SELECT UNIX_TIMESTAMP('2026-11-23 14:30:00');
-- 1795444200

SELECT FROM_UNIXTIME(1795444200);
-- 2026-11-23 14:30:00

FROM_UNIXTIME also returns its result in the session time zone, so the same query shows a different wall-clock time on a connection set to another zone. Setting the zone when the connection opens keeps every query consistent. In an application, it is usually simpler to convert in code and send MySQL a DATETIME value or a number.

SQLite

SQLite 3.38 and later have unixepoch(), which accepts ISO 8601 with Z or an offset:

SELECT unixepoch('2026-11-23T14:30:00Z');       -- 1795444200
SELECT unixepoch('2026-11-23T16:30:00+02:00');  -- 1795444200
SELECT datetime(1795444200, 'unixepoch');       -- 2026-11-23 14:30:00

On older versions, CAST(strftime('%s', '2026-11-23T14:30:00Z') AS INTEGER) does the same. SQLite has no date type of its own, so pick one storage format, ISO text or Unix integers, and use it in every table.

The shell

date behaves differently on Linux (GNU coreutils) and macOS (BSD), which is the usual reason a script works on one and fails on the other.

# Linux (GNU date)
date -d "2026-11-23T14:30:00Z" +%s                    # 1795444200
date -u -d @1795444200 +%Y-%m-%dT%H:%M:%SZ            # 2026-11-23T14:30:00Z

# macOS (BSD date)
date -j -u -f "%Y-%m-%dT%H:%M:%SZ" "2026-11-23T14:30:00Z" +%s   # 1795444200
date -u -r 1795444200 +%Y-%m-%dT%H:%M:%SZ                       # 2026-11-23T14:30:00Z

# Current time, both
date +%s

On macOS, the -u flag matters in the parse command too: without it, the time is read as local time and the result is off by your UTC offset. Installing GNU coreutils with Homebrew gives you gdate, which accepts the Linux syntax.

For JSON in a pipeline, jq has fromdateiso8601 and todateiso8601:

echo '"2026-11-23T14:30:00Z"' | jq 'fromdateiso8601'   # 1795444200
echo '1795444200' | jq 'todateiso8601'                  # "2026-11-23T14:30:00Z"

fromdateiso8601 accepts exactly YYYY-MM-DDTHH:MM:SSZ. A value with milliseconds, such as 2026-11-23T14:30:00.750Z, fails with "does not match format". Strip the fraction first, for example with sub("\\.[0-9]+Z$"; "Z").

Other languages

LanguageISO 8601 to Unix seconds
Got, err := time.Parse(time.RFC3339, iso) then t.Unix()
JavaOffsetDateTime.parse(iso).toEpochSecond()
C#DateTimeOffset.Parse(iso).ToUnixTimeSeconds()
PHP(new DateTimeImmutable($iso))->getTimestamp()
Rubyrequire "time" then Time.iso8601(iso).to_i

Each of these reads the offset from the string, so Z and +02:00 both work. For milliseconds, Go has t.UnixMilli(), Java has toInstant().toEpochMilli() and C# has ToUnixTimeMilliseconds().

Mistakes that cause wrong timestamps

Mixing seconds and milliseconds. A seconds value read as milliseconds lands in January 1970; a milliseconds value read as seconds lands tens of thousands of years in the future. Name fields after their unit (created_at_ms, expires_at_s), and when a value comes from outside, check the digit count. The timestamp converter treats values of 100,000,000,000 and above as milliseconds, which is a safe rule for any date after March 1973.

Strings without an offset. 2026-11-23T14:30:00 is a different instant in every time zone. Most "off by a few hours" bugs come from a string like this being parsed on a machine whose zone differs from the one that wrote it. Write ISO strings with Z or an offset everywhere you control.

Treating an offset as a time zone. +02:00 says how far that one timestamp was from UTC. It does not say which zone it came from, and it does not tell you the offset of a date in summer if the zone uses daylight saving time. To schedule something "at 9:00 every day in Beirut", store the zone name (Asia/Beirut) and compute each instant from it.

Comparing as text. ISO strings in UTC with the same precision sort correctly as text. Mix in offsets or optional milliseconds and they do not: 2026-11-23T14:30:00+02:00 sorts after 2026-11-23T13:00:00Z even though it is the earlier instant. Compare parsed values or Unix numbers instead.

32-bit storage. A signed 32-bit integer of seconds runs out at 2038-01-19T03:14:07Z. Use 64-bit integers (BIGINT) for Unix timestamps in databases and binary formats, even when today's values fit in 32 bits.

Questions that come up

What is the Unix timestamp of an ISO 8601 date?

The number of seconds between 1970-01-01T00:00:00Z and that instant. For 2026-11-23T14:30:00Z it is 1795444200. Paste any date into the Unix Timestamp Converter to get its value in seconds and milliseconds.

Is a Unix timestamp always UTC?

A Unix timestamp has no time zone at all: it counts seconds from an instant defined in UTC, so the same number means the same moment everywhere. Time zones only come in when you turn it into a calendar date and clock time.

How do I know whether a timestamp is in seconds or milliseconds?

Count the digits. Current dates are 10 digits in seconds and 13 in milliseconds. The units diverge so far that a wrong guess is obvious: the result lands in 1970 or in the far future.

Why does new Date("2026-11-23") show the 22nd?

The date-only form is read as midnight UTC. Displayed in a time zone behind UTC, such as anywhere in the Americas, midnight UTC is still the evening of the 22nd. Format it with timeZone: "UTC" or parse it as a local date if the value is a calendar date rather than an instant.

What is the difference between ISO 8601 and RFC 3339?

RFC 3339 is a strict profile of ISO 8601 for internet protocols. It requires a full date, a full time and an offset, and allows a space instead of the T. Valid RFC 3339 timestamps are what most APIs mean by "ISO 8601".

Sources and docs