View a markdown version of this page

Date arithmetic functions - Amazon Athena

Date arithmetic functions

Use these functions to add an interval to a date, a time, or a timestamp, or to measure the span between two values. You can also add an interval with the + (addition) operator, or subtract it with the - (subtraction and negation) operator.

age function

Returns the time from timestamp2 to timestamp1 as an INTERVAL in calendar years, months, and days, plus a time of day when the times of day differ. If timestamp1 is earlier than timestamp2, every part of the result is negative. If either argument is NULL, the result is NULL.

With one argument, age returns the time from timestamp to midnight at the start of the current date in UTC. The result of that form varies with the date on which you run the query.

Subtracting one TIMESTAMP from another also returns an INTERVAL, but that interval counts days and does not group them into months and years.

Syntax

age(timestamp1, timestamp2) age(timestamp)

Arguments

The age function takes the following arguments:

timestamp1

The later value, as a TIMESTAMP or TIMESTAMP WITH TIME ZONE. A DATE is read as midnight on that date.

timestamp2

The earlier value, of the same type as timestamp1.

timestamp

The value to measure from in the one-argument form, as a TIMESTAMP or TIMESTAMP WITH TIME ZONE value.

Return type

INTERVAL

Examples

The following SQL query returns the time between two timestamps as years, months, and days.

SELECT age( TIMESTAMP '2001-04-10', TIMESTAMP '1992-09-20' ) AS result

The query returns the following result.

result

8 years 6 months 20 days

The following SQL query compares age with subtracting the same two timestamps. Subtraction returns the span as a number of days.

SELECT age( TIMESTAMP '2001-04-10', TIMESTAMP '1992-09-20' ) AS age_result, TIMESTAMP '2001-04-10' - TIMESTAMP '1992-09-20' AS subtraction_result

The query returns the following result.

age_result subtraction_result

8 years 6 months 20 days

3124 days

The following SQL query passes the earlier timestamp first, so every part of the result is negative.

SELECT age( TIMESTAMP '1992-09-20', TIMESTAMP '2001-04-10' ) AS result

The query returns the following result.

result

-8 years -6 months -20 days

The following SQL query shows that the one-argument form measures to midnight at the start of the current date. The argument is that same midnight, so the result is a zero interval. The date varies from day to day, so the query compares the result instead of showing it.

SELECT age( CAST(current_date AS TIMESTAMP) ) = INTERVAL 0 DAY AS is_zero

The query returns the following result.

is_zero

true

date_add function

Returns value plus interval. date_add(value, interval) is a macro for value + interval, so it returns the same result and the same type as the + operator. If either argument is NULL, the result is NULL.

Adding an interval to a DATE returns a TIMESTAMP. When the interval has months or years and the day does not exist in the resulting month, the result is the last day of that month. To go back in time, pass a negative interval, such as INTERVAL (-1) DAY, or use the - operator.

Syntax

date_add(value, interval)

Arguments

The date_add function takes the following arguments:

value

The value to add to, as a DATE, TIMESTAMP, TIMESTAMP WITH TIME ZONE, or TIME value.

interval

The interval to add, as an INTERVAL. With a DATE, you can also pass an INTEGER number of days.

Return type

The type that + returns for the arguments: TIMESTAMP for a DATE plus an INTERVAL, DATE for a DATE plus an INTEGER, and otherwise the type of value.

Examples

The following SQL query adds one month to January 31. February has no 31st day, so the result is the last day of February, as a TIMESTAMP.

SELECT date_add( DATE '2026-01-31', INTERVAL 1 MONTH ) AS result

The query returns the following result.

result

2026-02-28 00:00:00

The following SQL query adds 7 days to a date and casts the result back to DATE.

SELECT CAST( date_add( DATE '2026-11-18', INTERVAL 7 DAY ) AS DATE ) AS result

The query returns the following result.

result

2026-11-25

The following SQL query adds 90 minutes to a timestamp. The result is on the next day.

SELECT date_add( TIMESTAMP '2026-11-18 22:30:00', INTERVAL 90 MINUTE ) AS result

The query returns the following result.

result

2026-11-19 00:00:00

The following SQL query adds an integer number of days to a date. The result is a DATE.

SELECT date_add(DATE '2026-11-18', 7) AS result

The query returns the following result.

result

2026-11-25

Considerations

date_add takes no unit argument. A call with a unit and three arguments, such as date_add('day', 1, value), fails.

Because date_add applies the + operator to whatever you pass, it does not check that the arguments are dates. For example, date_add(1, 1) returns 2.

Differences from Athena engine version 3

In Athena engine version 3, date_add takes a unit, a number, and a value, as in date_add('day', 1, ts). DuckDB does not accept that form, and the query fails. Write ts + INTERVAL 1 DAY or date_add(ts, INTERVAL 1 DAY) instead.

In Athena engine version 3, adding days to a DATE with date_add returns a DATE. In DuckDB, a DATE plus an INTERVAL returns a TIMESTAMP, so cast the result when you need a date, as in CAST(d + INTERVAL 3 DAY AS DATE).

date_diff function

Returns the number of times that a part boundary is crossed from startdate to enddate. For example, with 'month', January 31 to February 1 is 1 month, because the first day of a month comes between them. If enddate is earlier than startdate, the result is negative. If any argument is NULL, the result is NULL.

With 'week', the function counts the day boundaries and divides by 7, rounding toward zero, so a week is any 7 days and does not start on a particular weekday.

To count only whole units, use the date_sub function. datediff takes the same arguments and returns the same results.

Syntax

date_diff(part, startdate, enddate)

Arguments

The date_diff function takes the following arguments:

part

The name of the unit to count, as a VARCHAR value, such as 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', or 'second'. Case is ignored. For the full list of part names, see Date and time functions and operators in DuckDB.

startdate

The start value, as a DATE, TIME, TIMESTAMP, or TIMESTAMP WITH TIME ZONE.

enddate

The end value, of the same type as startdate. If you mix a DATE with a TIMESTAMP, the DATE is read as midnight on that date.

Return type

BIGINT

Examples

The following SQL query counts the hour boundaries from 23:59:59 to 01:58:00 on the next day. Less than two hours pass, but two hour boundaries are crossed, at 00:00 and at 01:00.

SELECT date_diff( 'hour', TIMESTAMPTZ '1992-09-30 23:59:59', TIMESTAMPTZ '1992-10-01 01:58:00' ) AS result

The query returns the following result.

result

2

The following SQL query compares date_diff with date_sub for January 31 to February 1. date_diff counts the month boundary, and date_sub finds no whole month.

SELECT date_diff( 'month', DATE '2021-01-31', DATE '2021-02-01' ) AS date_diff_result, date_sub( 'month', DATE '2021-01-31', DATE '2021-02-01' ) AS date_sub_result

The query returns the following result.

date_diff_result date_sub_result

1

0

The following SQL query passes an end date that is earlier than the start date, so the result is negative.

SELECT date_diff( 'day', DATE '2026-11-18', DATE '2026-11-11' ) AS result

The query returns the following result.

result

-7

The following SQL query counts the weeks from a Sunday to the next day, a Monday. Weeks are counted as 7 days, not as weeks that start on Monday, so the result is 0.

SELECT date_diff( 'week', DATE '2026-11-15', DATE '2026-11-16' ) AS result

The query returns the following result.

result

0

Considerations

For TIMESTAMP WITH TIME ZONE values, boundaries are counted in UTC, the session time zone, not in the time zone that the values were written with.

Differences from Athena engine version 3

In Athena engine version 3, date_diff counts the whole units that have passed between the two values. In DuckDB, date_diff counts the unit boundaries crossed, so the result can be larger. For example, from 23:00 on one day to 01:00 on the next, date_diff('day', ...) returns 1 in DuckDB, although less than a day has passed. To get the Athena engine version 3 result, use the date_sub function with the same arguments.

date_sub function

Returns how many whole part units fit in the span from startdate to enddate. A partial unit at the end is not counted, so 23:59:59 to 01:58:00 on the next day is 1 hour, and January 15 to February 14 is 0 months. If enddate is earlier than startdate, the result is negative. If any argument is NULL, the result is NULL.

To count the boundaries crossed instead, use the date_diff function. datesub takes the same arguments and returns the same results.

Syntax

date_sub(part, startdate, enddate)

Arguments

The date_sub function takes the following arguments:

part

The name of the unit to count, as a VARCHAR value, such as 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', or 'second'. Case is ignored. For the full list of part names, see Date and time functions and operators in DuckDB.

startdate

The start value, as a DATE, TIME, TIMESTAMP, or TIMESTAMP WITH TIME ZONE.

enddate

The end value, of the same type as startdate.

Return type

BIGINT

Examples

The following SQL query counts the whole hours from 23:59:59 to 01:58:00 on the next day.

SELECT date_sub( 'hour', TIMESTAMPTZ '1992-09-30 23:59:59', TIMESTAMPTZ '1992-10-01 01:58:00' ) AS result

The query returns the following result.

result

1

The following SQL query shows that a month is not complete until the same day of the next month.

SELECT date_sub( 'month', DATE '2021-01-15', DATE '2021-02-14' ) AS before_full_month, date_sub( 'month', DATE '2021-01-15', DATE '2021-02-15' ) AS full_month

The query returns the following result.

before_full_month full_month

0

1

The following SQL query starts on January 31. February has no 31st day, so a whole month is complete on February 28.

SELECT date_sub( 'month', DATE '2021-01-31', DATE '2021-02-28' ) AS result

The query returns the following result.

result

1

Considerations

date_sub does not subtract an interval from a date, as functions of this name do in some other SQL engines. A call such as date_sub(DATE '2026-11-18', INTERVAL 1 DAY) fails. To subtract an interval, use the - operator, as in DATE '2026-11-18' - INTERVAL 1 DAY.

When enddate is the last day of its month, a month is complete on that day, even if the day of the month of startdate is later. For example, date_sub('month', DATE '2021-01-30', DATE '2021-02-28') and date_sub('month', DATE '2021-01-31', DATE '2021-02-28') both return 1, but date_sub('month', DATE '2021-01-31', DATE '2021-02-27') returns 0.

Differences from Athena engine version 3

date_sub counts whole units in the same way as date_diff in Athena engine version 3. When you move a query from Athena engine version 3, replace date_diff(part, startdate, enddate) with date_sub(part, startdate, enddate) to keep the same results.