Request a demo

BigQuery · Datetime function

BigQuery DATETIME_DIFF

DATETIME_DIFF(end, start, part) returns the number of part boundaries between two DATETIME values as INT64. Unlike TIMESTAMP_DIFF, it counts boundaries for every part: 23:55 to 00:05 is 1 DAY and 1 HOUR.

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

DATETIME_DIFF subtracts start from end in a unit from MICROSECOND up to YEAR, including WEEK, WEEK(<WEEKDAY>), ISOWEEK, MONTH, QUARTER and ISOYEAR. The later value goes first; reversed arguments give 0 or a negative number.

The result is the number of unit boundaries crossed, not the number of complete units. From 23:55 to 00:05 the next day is 10 minutes, but it crosses one midnight and one hour mark, so DAY and HOUR both return 1. TIMESTAMP_DIFF counts complete units instead and returns 0 for the same pair. If you want elapsed hours on DATETIME values, compute a smaller unit and divide: DIV(DATETIME_DIFF(end, start, MINUTE), 60).

DATETIME has no time zone, so a DST change is invisible: noon to noon on the day clocks fall back is 24 hours here and 25 hours with TIMESTAMP values. Passing two TIMESTAMP values makes DATETIME_DIFF behave like TIMESTAMP_DIFF.

Syntax

DATETIME_DIFF(end_datetime, start_datetime, granularity)

Arguments and return value

ArgumentDescription
end_datetimeThe later DATETIME.
start_datetimeThe earlier DATETIME.
granularityMICROSECOND, MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK, WEEK(<WEEKDAY>), ISOWEEK, MONTH, QUARTER, YEAR or ISOYEAR.

Returns: INT64

Examples

Hours and minutes between two local times

9 hours 15 minutes: the hour count crosses nine hour marks (10:00 through 18:00).

SELECT
  DATETIME_DIFF(DATETIME '2026-11-23 18:45:00', DATETIME '2026-11-23 09:30:00', HOUR) AS hours,
  DATETIME_DIFF(DATETIME '2026-11-23 18:45:00', DATETIME '2026-11-23 09:30:00', MINUTE) AS minutes;
Result
hoursminutes
9555

Boundaries, not elapsed time

SELECT
  DATETIME_DIFF(DATETIME '2026-11-24 00:05:00', DATETIME '2026-11-23 23:55:00', DAY) AS datetime_days,
  DATETIME_DIFF(DATETIME '2026-11-24 00:05:00', DATETIME '2026-11-23 23:55:00', HOUR) AS datetime_hours,
  TIMESTAMP_DIFF(TIMESTAMP '2026-11-24 00:05:00+00', TIMESTAMP '2026-11-23 23:55:00+00', HOUR) AS timestamp_hours;
Result
datetime_daysdatetime_hourstimestamp_hours
110

DST: wall-clock hours versus real hours

New York gains an hour on November 1, 2026.

SELECT
  DATETIME_DIFF(DATETIME '2026-11-01 12:00:00', DATETIME '2026-10-31 12:00:00', HOUR) AS wall_clock_hours,
  TIMESTAMP_DIFF(TIMESTAMP '2026-11-01 12:00:00 America/New_York', TIMESTAMP '2026-10-31 12:00:00 America/New_York', HOUR) AS elapsed_hours;
Result
wall_clock_hourselapsed_hours
2425

Elapsed whole hours for shift lengths

Shift a lasted 8 h 20 min but crosses nine hour marks (09:00 to 17:00).

WITH shifts AS (
  SELECT 'a' AS shift_id, DATETIME '2026-11-23 08:50:00' AS clock_in, DATETIME '2026-11-23 17:10:00' AS clock_out UNION ALL
  SELECT 'b', DATETIME '2026-11-23 22:00:00', DATETIME '2026-11-24 05:59:00'
)
SELECT
  shift_id,
  DATETIME_DIFF(clock_out, clock_in, HOUR) AS hour_marks_crossed,
  DIV(DATETIME_DIFF(clock_out, clock_in, MINUTE), 60) AS full_hours_worked
FROM shifts
ORDER BY shift_id;
Result
shift_idhour_marks_crossedfull_hours_worked
a98
b77

Common mistakes

  • Expecting the same results as TIMESTAMP_DIFF. For HOUR, MINUTE and DAY, DATETIME_DIFF counts boundaries and TIMESTAMP_DIFF counts complete units; the same pair of values can differ by 1.
  • Using Snowflake or SQL Server order. DATEDIFF(hour, start, end) doesn't exist; write DATETIME_DIFF(end, start, HOUR).
  • Measuring durations of real events with DATETIME. Across DST changes the result is off by an hour; store events as TIMESTAMP and use TIMESTAMP_DIFF.

Cost and performance

  • DATETIME_DIFF is on BigQuery's list of functions that can wrap a partitioning column without blocking partition pruning, provided the other arguments are constants. A direct range filter on the column remains the safest choice.

FAQ

How do I get the difference between two datetimes in hours?

DATETIME_DIFF(end, start, HOUR) counts hour marks crossed. For complete hours use DIV(DATETIME_DIFF(end, start, MINUTE), 60).

Why do DATETIME_DIFF and TIMESTAMP_DIFF give different answers?

DATETIME_DIFF counts unit boundaries for every part; TIMESTAMP_DIFF counts whole elapsed units. From 23:55 to 00:05, DATETIME_DIFF(..., DAY) is 1 and TIMESTAMP_DIFF(..., DAY) is 0.

Can DATETIME_DIFF return months between two datetimes?

Yes. DATETIME_DIFF(end, start, MONTH) counts month boundaries, so January 31 to February 1 is 1.

Errors you may hit

More datetime functions

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