Skip to main content

DATE_DIFF

Introduced or updated: v1.2.723

Calculates the difference between two dates or timestamps based on a specified time unit. The result is positive if the <end_date> is after the <start_date>, and negative if it's before.

See also: DATE_BETWEEN

Syntax

DATE_DIFF(
YEAR | QUARTER | MONTH | WEEK | DAY | HOUR | MINUTE | SECOND |
DOW | DOY | EPOCH | ISODOW | YEARWEEK | MILLENNIUM,
<start_date_or_timestamp>,
<end_date_or_timestamp>
)
KeywordDescription
DOWDay of the Week. Sunday (0) through Saturday (6).
DOYDay of the Year. 1 through 366.
EPOCHThe number of seconds since 1970-01-01 00:00:00.
ISODOWISO Day of the Week. Monday (1) through Sunday (7).
YEARWEEKThe year and week number combined, following ISO 8601 (e.g., 202415).
MILLENNIUMThe millennium of the date (1 for years 1–1000, 2 for 1001–2000, etc.).

DATE_DIFF vs. DATE_BETWEEN

The DATE_DIFF function counts how many boundaries of a user-specified unit (such as day, month, or year) are crossed between two dates, while DATE_BETWEEN counts how many complete units fall strictly between them. For example:

SELECT
DATE_DIFF(month, '2025-07-31', '2025-10-01'), -- returns 3
DATE_BETWEEN(month, '2025-07-31', '2025-10-01'); -- returns 2

In this example, DATE_DIFF returns 3 because the range crosses three month boundaries (July → August → September → October), while DATE_BETWEEN returns 2 because there are two full months between the dates: August and September.

Examples

This example calculates the difference between a fixed timestamp (2020-01-01 00:00:00) and the current timestamp (NOW()), across various units such as year, ISO weekday, year-week, and millennium:

SELECT
DATE_DIFF(YEAR, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_year,
DATE_DIFF(QUARTER, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_quarter,
DATE_DIFF(MONTH, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_month,
DATE_DIFF(WEEK, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_week,
DATE_DIFF(DAY, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_day,
DATE_DIFF(HOUR, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_hour,
DATE_DIFF(MINUTE, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_minute,
DATE_DIFF(SECOND, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_second,
DATE_DIFF(DOW, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_dow,
DATE_DIFF(DOY, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_doy,
DATE_DIFF(EPOCH, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_epoch,
DATE_DIFF(ISODOW, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_isodow,
DATE_DIFF(YEARWEEK, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_yearweek,
DATE_DIFF(MILLENNIUM, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_millennium;
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ diff_year │ diff_quarter │ diff_month │ diff_week │ diff_day │ diff_hour │ diff_minute │ diff_second │ diff_dow │ diff_doy │ diff_epoch │ diff_isodow │ diff_yearweek │ diff_millennium │
├───────────┼──────────────┼────────────┼───────────┼──────────┼───────────┼─────────────┼─────────────┼──────────┼──────────┼────────────┼─────────────┼───────────────┼─────────────────┤
5216327619324638627831841669910691932193216699106919325150
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
Explore Databend Cloud for FREE
Low-cost
Fast Analytics
Easy Data Ingestion
Elastic Scaling
Try it today