BigQuery · Date function
BigQuery UNIX_DATE
UNIX_DATE(date) returns the number of days since 1970-01-01 as INT64: UNIX_DATE(DATE '2026-11-23') is 20780. Use it for integer day keys and for formats that store dates as days since the Unix epoch.
UNIX_DATE counts the days between the Unix epoch, 1970-01-01, and a DATE. Dates before the epoch give negative numbers. The inverse is DATE_FROM_UNIX_DATE.
The result is handy where an integer is easier than a DATE: as an input to MOD for alternating schedules, as a compact join key, or when writing to systems that store dates as day numbers (Avro's date logical type uses the same days-since-epoch count).
For the gap between two dates, UNIX_DATE(a) - UNIX_DATE(b) equals DATE_DIFF(a, b, DAY). For seconds or milliseconds since the epoch, use UNIX_SECONDS or UNIX_MILLIS on a TIMESTAMP.
Syntax
UNIX_DATE(date_expression)Arguments and return value
| Argument | Description |
|---|---|
date_expression | The DATE to convert. NULL returns NULL. |
Returns: INT64: days since 1970-01-01.
Examples
Days since the epoch
SELECT
UNIX_DATE(DATE '2026-11-23') AS today_number,
UNIX_DATE(DATE '1970-01-01') AS epoch,
UNIX_DATE(DATE '1969-12-31') AS before_epoch;| today_number | epoch | before_epoch |
|---|---|---|
| 20780 | 0 | -1 |
Same answer as DATE_DIFF in days
SELECT
UNIX_DATE(DATE '2026-11-23') - UNIX_DATE(DATE '2026-11-01') AS by_unix_date,
DATE_DIFF(DATE '2026-11-23', DATE '2026-11-01', DAY) AS by_date_diff;| by_unix_date | by_date_diff |
|---|---|
| 22 | 22 |
Alternating on-call rotation
20780 is even, so team_a takes November 23. The rotation never resets at month or year boundaries.
WITH days AS (
SELECT day FROM UNNEST(GENERATE_DATE_ARRAY('2026-11-23', '2026-11-26')) AS day
)
SELECT
day,
IF(MOD(UNIX_DATE(day), 2) = 0, 'team_a', 'team_b') AS on_call
FROM days
ORDER BY day;| day | on_call |
|---|---|
| 2026-11-23 | team_a |
| 2026-11-24 | team_b |
| 2026-11-25 | team_a |
| 2026-11-26 | team_b |
Common mistakes
- Expecting seconds. UNIX_DATE counts days;
UNIX_SECONDS(TIMESTAMP(d))gives seconds at midnight UTC. - Passing a TIMESTAMP. UNIX_DATE takes a DATE; convert with
DATE(ts, 'Europe/London')first, choosing the time zone deliberately. - Using
MODon negative day numbers.MOD(-1, 2)is -1, not 1, so rotations that include dates before 1970 needABSor an offset.
FAQ
How do I convert a date to an integer in BigQuery?
UNIX_DATE(d) returns days since 1970-01-01. For a YYYYMMDD number use CAST(FORMAT_DATE('%Y%m%d', d) AS INT64).
What is the opposite of UNIX_DATE?
DATE_FROM_UNIX_DATE(n) turns a day count back into a DATE.
Related
More date functions
All BigQuery date functions · All functions · BigQuery error messages
