BigQuery · Timestamp function
BigQuery UNIX_MILLIS
UNIX_MILLIS(ts) returns the number of milliseconds since 1970-01-01 00:00:00 UTC as INT64, truncating microseconds. Use it to send timestamps to JavaScript, Kafka or APIs that expect epoch milliseconds.
UNIX_MILLIS converts a TIMESTAMP to Unix epoch milliseconds. BigQuery timestamps have microsecond precision, and UNIX_MILLIS rounds down to the start of the millisecond, so .123456 becomes .123. The inverse is TIMESTAMP_MILLIS.
Typical uses are exports to systems that store epoch milliseconds, joins with event tables that keep 13-digit integers, and precise durations: UNIX_MILLIS(end_ts) - UNIX_MILLIS(start_ts) equals TIMESTAMP_DIFF(end_ts, start_ts, MILLISECOND) when both timestamps are whole milliseconds.
For seconds use UNIX_SECONDS; for full precision use UNIX_MICROS.
Syntax
UNIX_MILLIS(timestamp_expression)Arguments and return value
| Argument | Description |
|---|---|
timestamp_expression | The TIMESTAMP to convert. NULL returns NULL. |
Returns: INT64
Examples
Epoch milliseconds, microseconds truncated
SELECT
UNIX_MILLIS(TIMESTAMP '2026-11-23 00:00:00+00') AS epoch_ms,
UNIX_MILLIS(TIMESTAMP '2026-11-23 00:00:00.123987+00') AS truncated_ms;| epoch_ms | truncated_ms |
|---|---|
| 1795392000000 | 1795392000123 |
Join a TIMESTAMP table with a millisecond event feed
WITH orders AS (
SELECT 'o1' AS order_id, TIMESTAMP '2026-11-23 01:02:03.456+00' AS created_at
),
clicks AS (
SELECT 'c1' AS click_id, 1795395723456 AS click_ms UNION ALL
SELECT 'c2', 1795395723999
)
SELECT o.order_id, c.click_id
FROM orders AS o
JOIN clicks AS c ON c.click_ms = UNIX_MILLIS(o.created_at);| order_id | click_id |
|---|---|
| o1 | c1 |
Durations in milliseconds
SELECT
UNIX_MILLIS(TIMESTAMP '2026-11-23 10:00:01.250+00') - UNIX_MILLIS(TIMESTAMP '2026-11-23 10:00:00+00') AS latency_ms;| latency_ms |
|---|
| 1250 |
Common mistakes
- Expecting rounding. UNIX_MILLIS truncates: 00:00:00.123987 becomes ...123, not ...124.
- Dividing by 1000 to get seconds.
UNIX_MILLIS(ts) / 1000returns FLOAT64; use UNIX_SECONDS for an integer. - Porting
EXTRACT(EPOCH FROM ts) * 1000from PostgreSQL. EXTRACT has no EPOCH part in BigQuery.
FAQ
How do I get epoch milliseconds from a timestamp in BigQuery?
UNIX_MILLIS(ts) returns an INT64 such as 1795392000000.
How do I convert epoch milliseconds back to a timestamp?
TIMESTAMP_MILLIS(ms).
Related
Errors you may hit
More timestamp functions
All BigQuery timestamp functions · All functions · BigQuery error messages
