Request a demo

BigQuery · Timestamp function

BigQuery TIMESTAMP_DIFF

TIMESTAMP_DIFF(end_ts, start_ts, part) returns the number of whole MICROSECOND, MILLISECOND, SECOND, MINUTE, HOUR or DAY units between two TIMESTAMPs as INT64, truncated toward zero. Use it for durations, latencies and freshness.

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

TIMESTAMP_DIFF subtracts start_ts from end_ts and counts complete units of the chosen part. 9 hours 59 minutes is 9 HOUR, and 23:55 to 00:05 the next day is 0 DAY. Results are truncated toward zero, so -3.9 hours is -3. The later timestamp goes first; reversed arguments give a negative number.

DAY is 24 hours, so a calendar day that contains a DST change still counts as 23 or 25 hours in HOUR. WEEK, MONTH, QUARTER and YEAR are not supported on TIMESTAMP and fail with "TIMESTAMP_DIFF does not support the MONTH date part when the argument is TIMESTAMP type". For calendar differences, convert to DATE or DATETIME in a zone and use DATE_DIFF or DATETIME_DIFF, which count boundaries.

For sub-second precision, diff in MILLISECOND or MICROSECOND and divide: TIMESTAMP_DIFF(end_ts, start_ts, MILLISECOND) / 1000 returns FLOAT64 seconds.

Syntax

TIMESTAMP_DIFF(end_timestamp, start_timestamp, granularity)

Arguments and return value

ArgumentDescription
end_timestampThe later TIMESTAMP.
start_timestampThe earlier TIMESTAMP.
granularityMICROSECOND, MILLISECOND, SECOND, MINUTE, HOUR or DAY (24 hours).

Returns: INT64

Examples

Duration in several units

SELECT
  TIMESTAMP_DIFF(TIMESTAMP '2026-11-23 18:45:00+00', TIMESTAMP '2026-11-23 09:30:00+00', HOUR) AS hours,
  TIMESTAMP_DIFF(TIMESTAMP '2026-11-23 18:45:00+00', TIMESTAMP '2026-11-23 09:30:00+00', MINUTE) AS minutes,
  TIMESTAMP_DIFF(TIMESTAMP '2026-11-23 18:45:00+00', TIMESTAMP '2026-11-23 09:30:00+00', SECOND) AS seconds;
Result
hoursminutesseconds
955533300

Whole units, not calendar boundaries

SELECT
  TIMESTAMP_DIFF(TIMESTAMP '2026-11-24 00:05:00+00', TIMESTAMP '2026-11-23 23:55:00+00', DAY) AS timestamp_days,
  TIMESTAMP_DIFF(TIMESTAMP '2026-11-24 00:05:00+00', TIMESTAMP '2026-11-23 23:55:00+00', MINUTE) AS minutes,
  DATE_DIFF(DATE '2026-11-24', DATE '2026-11-23', DAY) AS date_days;
Result
timestamp_daysminutesdate_days
0101

A DST day lasts 25 hours

SELECT TIMESTAMP_DIFF(
  TIMESTAMP '2026-11-02 00:00:00 America/New_York',
  TIMESTAMP '2026-11-01 00:00:00 America/New_York',
  HOUR) AS hours_in_nov_1;
Result
hours_in_nov_1
25

Pipeline run durations

SECOND truncates 59.9 to 59. Diff in MILLISECOND and divide when fractions matter.

WITH runs AS (
  SELECT 'run_1' AS run_id, TIMESTAMP '2026-11-23 10:00:00+00' AS started_at, TIMESTAMP '2026-11-23 10:12:30+00' AS finished_at UNION ALL
  SELECT 'run_2', TIMESTAMP '2026-11-23 11:00:00+00', TIMESTAMP '2026-11-23 11:00:59.900+00'
)
SELECT
  run_id,
  TIMESTAMP_DIFF(finished_at, started_at, SECOND) AS seconds,
  TIMESTAMP_DIFF(finished_at, started_at, MILLISECOND) / 1000 AS precise_seconds
FROM runs
ORDER BY run_id;
Result
run_idsecondsprecise_seconds
run_1750750.0
run_25959.9

Common mistakes

  • Using WEEK, MONTH or YEAR. They fail on TIMESTAMP; convert with DATE(ts, zone) and use DATE_DIFF.
  • Expecting DAY to count midnights. TIMESTAMP_DIFF(..., DAY) is elapsed 24-hour periods; 23:55 to 00:05 is 0. Use DATE_DIFF(DATE(end_ts, zone), DATE(start_ts, zone), DAY) for calendar days.
  • Swapping arguments. BigQuery puts the end first (TIMESTAMP_DIFF(end, start, part)), the reverse of Snowflake's and SQL Server's DATEDIFF(part, start, end), so ported code comes out negative.
  • Using SECOND for latencies under a second. Everything below one second becomes 0; use MILLISECOND.

Cost and performance

  • TIMESTAMP_ADD, TIMESTAMP_SUB, TIMESTAMP_DIFF and TIMESTAMP_TRUNC are on BigQuery's list of functions that can wrap a partitioning column and still allow partition pruning when their other arguments are constants. Arithmetic with operators (ts + INTERVAL 1 DAY > ...) is not; move the arithmetic to the constant side: ts > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 1 DAY).
  • Freshness checks that run every few minutes are cheap only if they read little data: compute MAX(loaded_at) from a partitioned or small metadata table, or from INFORMATION_SCHEMA, not from the full fact table.

FAQ

How do I get the difference between two timestamps in BigQuery?

TIMESTAMP_DIFF(end_ts, start_ts, SECOND) returns whole seconds as INT64. Use MINUTE, HOUR or DAY for larger units.

Why does TIMESTAMP_DIFF return 0 days for timestamps on different dates?

It counts complete 24-hour periods. Ten minutes across midnight is 0 days; use DATE_DIFF on the dates to count calendar days.

How do I get the number of months between two timestamps?

Convert to dates first: DATE_DIFF(DATE(end_ts), DATE(start_ts), MONTH). TIMESTAMP_DIFF doesn't support MONTH.

How do I get fractional seconds between timestamps?

TIMESTAMP_DIFF(end_ts, start_ts, MILLISECOND) / 1000 returns FLOAT64 seconds.

Errors you may hit

More timestamp functions

All BigQuery timestamp 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.