Request a demo

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.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

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

ArgumentDescription
date_expressionThe 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;
Result
today_numberepochbefore_epoch
207800-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;
Result
by_unix_dateby_date_diff
2222

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;
Result
dayon_call
2026-11-23team_a
2026-11-24team_b
2026-11-25team_a
2026-11-26team_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 MOD on negative day numbers. MOD(-1, 2) is -1, not 1, so rotations that include dates before 1970 need ABS or 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.

More date functions

All BigQuery date functions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.