Shape / columns · All operations

Find the time between two dates

A variant of Parse a date or time

Compute the interval or the count of units between two dates or times.

Status

In arcform

Runs as a SQL step

arcform carries it as a sql: step today.

The DuckDB step

Count the days, months and time between two dates

It reads the orders table the proofs share. Create it first:

SQL
CREATE TABLE orders AS SELECT * FROM (VALUES
  (1,  'Alice Ng',      'north', DATE '2026-01-03', 120.00, 'widget', 'a|b',   '{"channel":"web","items":[1,2]}'),
  (2,  'alice ng',      'north', DATE '2026-01-05',  80.50, 'gadget', 'b',     '{"channel":"shop","items":[3]}'),
  (3,  'Bob  Okafor',   'south', DATE '2026-01-05', 300.00, 'widget', 'a|c',   '{"channel":"web","items":[4,5,6]}'),
  (4,  'Carla Díaz',    'east',  DATE '2026-01-09',  NULL,  'gizmo',  NULL,    '{"channel":"web","items":[]}'),
  (5,  'Bob Okafor',    'south', DATE '2026-01-12',  45.25, 'gadget', 'c',     '{"channel":"phone","items":[7]}'),
  (6,  'Dev Patel',     'west',  DATE '2026-01-12', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (7,  'Carla Diaz',    'east',  DATE '2026-01-20',  99.99, 'gizmo',  'b|c',   '{"channel":"shop","items":[10]}'),
  (8,  'Dev Patel',     'west',  DATE '2026-02-02', 510.00, 'widget', 'a',     '{"channel":"web","items":[8,9]}'),
  (9,  'Eve Larsen',    NULL,    DATE '2026-02-03',  15.00, 'gadget', NULL,    '{"channel":"web","items":[11]}'),
  (10, 'Alice Ng',      'north', DATE '2026-02-10', 220.00, 'gizmo',  'a|b|c', '{"channel":"phone","items":[12,13]}')
) t(order_id, customer, region, order_date, amount, product, tags, meta);

Then run the statement:

SQL
CREATE TEMP TABLE result AS
SELECT order_id, order_date - DATE '2026-01-03' AS days_since_first FROM orders;
SELECT * FROM result;
CREATE TEMP TABLE units AS
SELECT date_diff('month', DATE '2026-01-31', DATE '2026-02-01') AS month_boundaries, date_sub('month', DATE '2026-01-31', DATE '2026-02-01') AS whole_months,
       TIMESTAMP '2026-02-10 14:30:00' - TIMESTAMP '2026-02-10 12:00:00' AS elapsed;
SELECT * FROM units;
SELECT CASE WHEN ((SELECT days_since_first FROM result WHERE order_id = 10) = 38 AND (SELECT max(days_since_first) FROM result) = 38 AND (SELECT month_boundaries = 1 AND whole_months = 0 AND elapsed = INTERVAL 150 MINUTE FROM units)) THEN 'as expected' ELSE error('the gaps were not 38 days, one month boundary, no whole month and two and a half hours') END AS verdict;
  • It needs nothing installed beyond DuckDB itself.
  • The statement checks its own result and raises an error when it is wrong: “the gaps were not 38 days, one month boundary, no whole month and two and a half hours”.
  • It passed on DuckDB v2.0.0 (Cyanoptera) d54cbbe3df.

What other tools call it

Meridian's classification; an independent blind rating agreed on 95.7% of 324 rows (variant, version 3).

Duckle (1)
  • Date Diffread 2026-09-25

    difference between two dates

Ibis (6)
  • DateDeltaread 2026-09-27

    Difference between two dates as a whole number of a chosen unit

  • DateDiffread 2026-09-27

    Difference between two dates, as an interval

  • TimeDeltaread 2026-09-27

    Difference between two times as a whole number of a chosen unit

  • TimeDiffread 2026-09-27

    Difference between two times, as an interval

  • TimestampDeltaread 2026-09-27

    Difference between two timestamps as a whole number of a chosen unit

  • TimestampDiffread 2026-09-27

    Difference between two timestamps, as an interval

KNIME Analytics Platform (1)
Polars (1)
  • business_day_countread 2026-09-27

    Count the business days between a start and an end date, end excluded

Background

In DuckDB, subtracting two TIMESTAMPs gives an INTERVAL describing their difference, while datediff counts the partition boundaries between them for a unit.[1] In DuckDB, subtracting one DATE from another returns the number of days between them as an integer rather than an interval.[1]

When to use it

In DuckDB, date_diff('month', DATE '1992-09-15', DATE '1992-11-14') counts month boundaries and returns 2, while date_sub, which counts whole months, returns 1 for the same dates.[2] Decide whether the question counts calendar periods crossed or whole periods elapsed before choosing the function: a person's age in whole years is the second kind. (Meridian's view)

In PostgreSQL, subtracting timestamptz '2013-03-01 12:00:00' from timestamptz '2013-07-01 12:00:00' can give 121 days 23:00:00, while age gives 4 mons.[3] PostgreSQL's age function uses the month of the earlier date when calculating partial months, so age('2004-06-01', '2004-04-30') yields 1 mon 1 day.[3] Choose between an elapsed difference and a calendar one deliberately whenever the two values lie either side of a clock change or a month end. (Meridian's view)

The method

In DuckDB, subtracting two TIMESTAMP values gives an INTERVAL with only days and microseconds components.[1] DuckDB's date_diff returns the signed number of part boundaries between two values, inclusive of the larger value and exclusive of the smaller.[4]

PostgreSQL's age performs field-by-field subtraction, giving a symbolic result that uses years and months rather than just days.[3] In PostgreSQL, converting each value to seconds with EXTRACT(EPOCH FROM ...) and subtracting gives the seconds between them, adjusting for month lengths, time zone changes and daylight saving time.[3]

Compared with the alternatives

DuckDB's date_sub truncates the interval between two values to whole multiples of the part, so for 1992-09-30 23:59:59 and 1992-10-01 01:58:00 it returns 1 hour where date_diff returns 2.[4]