View a markdown version of this page

Date and time functions and operators in DuckDB - Amazon Athena

Date and time functions and operators in DuckDB

Use date and time functions and operators to get the current date and time and to read parts of a value, such as the year or the hour. You can also truncate values to a unit, add intervals, and count the units between two values.

The main date and time types in DuckDB are DATE, TIME, TIME WITH TIME ZONE, TIMESTAMP, TIMESTAMP WITH TIME ZONE, and INTERVAL. A DATE is a calendar date. A TIME is a time of day without a date. A TIME WITH TIME ZONE, which you can also write as TIMETZ, is a time of day that keeps the UTC offset that you write. A TIMESTAMP is a date and time without a time zone. A TIMESTAMP WITH TIME ZONE, which you can also write as TIMESTAMPTZ, is an instant in time. An INTERVAL is a length of time. Write literals with the type name, as in DATE '2026-11-18', TIME '10:30:00', TIMESTAMP '2026-11-18 10:30:00', TIMESTAMPTZ '2026-11-18 10:30:00-08', INTERVAL 90 MINUTE, and INTERVAL '2 hours 30 minutes'. The date and time functions do not convert a VARCHAR value, such as a string column, to a date for you. They convert a string literal such as '2026-11-18' only in some calls where another argument already has a date or time type, as in date_diff('day', DATE '2026-11-01', '2026-11-18'). Write a typed literal or cast the string, as in CAST(order_day AS DATE).

The session time zone is UTC, and you cannot change it. A TIMESTAMP WITH TIME ZONE value does not keep the offset that it was written with. It is returned in UTC, and functions that read its parts, such as the day or the hour, read them in UTC. The functions that return the current date or time also use UTC.

Functions such as date_part, date_trunc, date_diff, and date_sub take the part as a string, such as 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second', 'millisecond', or 'microsecond'. All four also accept 'decade', 'century', and 'millennium'. The date_part function also accepts 'era', 'dow' (the day of the week, with Sunday as 0), and 'doy' (the day of the year). For ISO weeks, it accepts 'isodow' (the ISO day of the week, with Monday as 1 and Sunday as 7), 'isoyear', and 'yearweek'. It also accepts 'epoch' (seconds since 1970-01-01 00:00:00 UTC), 'julian', and 'timezone'. Part names are not case-sensitive, and plural and short forms such as 'days', 'd', and 'mon' also work.

Use + and - to add and subtract intervals. A DATE plus or minus an INTERVAL returns a TIMESTAMP. A DATE plus or minus an INTEGER returns the DATE that many days later or earlier. The difference of two DATE values is a number of days as a BIGINT. The difference of two TIMESTAMP values is an INTERVAL. To add a BIGINT number of days to a DATE, cast it to INTEGER first. When you add months or years and the day does not exist in the resulting month, the result is the last day of that month. For example, DATE '2026-01-31' + INTERVAL 1 MONTH returns 2026-02-28 00:00:00.

An INTERVAL keeps its months, days, and time of day separately, so INTERVAL 36 HOUR is not changed to 1 day and 12 hours. Hive tables and Iceberg tables cannot store INTERVAL values, so convert an interval before you write it to a table, for example to a number of seconds with date_part('epoch', interval).

The following table lists the date and time functions and operators, with a link to the description of each one.

Function Description

age

Returns the time between two timestamps as an interval in years, months, and days.

ago

Returns the current date and time minus an interval.

century

Returns the century of a date, a timestamp, or an interval.

current_date

Returns the current date in UTC.

current_localtime

Returns the current time of day in UTC, without a time zone.

current_localtimestamp

Returns the current date and time in UTC, without a time zone.

date_add

Adds an interval to a date, a timestamp, or a time.

date_diff

Counts how many times the span from one date or timestamp to another crosses the start of a unit, such as a month or an hour.

date_part

Returns a part of a date, a time, a timestamp, or an interval, such as the year or the hour.

date_sub

Counts the whole units, such as whole months or whole hours, that fit in the span from one date or timestamp to another.

date_trunc

Truncates a date, a timestamp, or an interval to the start of a part, such as the month or the hour.

datediff

Alias of date_diff.

datepart

Alias of date_part.

datesub

Alias of date_sub.

datetrunc

Alias of date_trunc.

day

Returns the day of the month of a date or a timestamp, or the days part of an interval.

dayname

Returns the day of the week of a date or a timestamp as an English word, such as Monday.

dayofmonth

Alias of day.

dayofweek

Returns the day of the week as a number from 0 for Sunday to 6 for Saturday.

dayofyear

Returns the day of the year, from 1 to 366, of a date or timestamp.

days_in_month

Returns the number of days in the month of a date or timestamp.

decade

Returns the decade of a date or timestamp, which is the year divided by 10.

epoch

Returns the number of seconds since 1970-01-01 00:00:00 UTC, or the length of an interval in seconds.

epoch_ms

Returns the number of milliseconds since 1970-01-01 00:00:00 UTC, or converts a number of milliseconds to a timestamp.

epoch_ns

Returns the number of nanoseconds since 1970-01-01 00:00:00 UTC, or the length of an interval in nanoseconds.

epoch_us

Returns the number of microseconds since 1970-01-01 00:00:00 UTC, or the length of an interval in microseconds.

era

Returns the era of a date or timestamp: 1 for AD and 0 for BC.

get_current_time

Returns the current time of day with a time zone.

get_current_timestamp

Returns the current date and time with a time zone.

hour

Returns the hour, from 0 to 23, of a time or timestamp.

isodow

Returns the ISO 8601 day of the week, from 1 for Monday to 7 for Sunday.

isoyear

Returns the ISO 8601 week-numbering year of a date or timestamp.

julian

Converts a date or timestamp to its Julian day number, with the time of day as a fraction.

last_day

Returns the date of the final day in the month that contains a date or timestamp.

make_date

Builds a date from a year, month, and day, from a struct of those parts, or from a number of days since 1970-01-01.

make_time

Builds a time of day from an hour, a minute, and seconds.

make_timestamp

Builds a timestamp from its date and time parts, or from a number of microseconds since 1970-01-01 00:00:00.

make_timestamp_ms

Converts a number of milliseconds since 1970-01-01 00:00:00 to a timestamp.

make_timestamp_ns

Converts a number of nanoseconds since 1970-01-01 00:00:00 to a nanosecond-precision timestamp.

make_timestamptz

Builds a timestamp with time zone from its date and time parts and an optional time zone, or from a number of microseconds since 1970-01-01 00:00:00 UTC.

microsecond

Returns the seconds and fractional seconds of a timestamp, time, or interval, in microseconds.

millennium

Returns the millennium, counted from year 1, that a date or timestamp falls in, or the number of whole millennia in an interval.

millisecond

Returns the seconds and fractional seconds of a timestamp, time, or interval, in milliseconds.

minute

Returns the minute of a timestamp or time, or the minutes part of an interval.

month

Returns the number of the month, from 1 to 12, that a date or timestamp falls in, or the months part of an interval.

monthname

Returns the month of a date or timestamp as a full English word, such as September.

nanosecond

Returns the seconds and fractional seconds of a timestamp, time, or interval, in nanoseconds.

normalized_interval

Rewrites an interval so that whole 24-hour periods become days and whole 30-day periods become months.

now

Alias of get_current_timestamp.

pg_timezone_names

Returns one row for each time zone name that you can use, with its abbreviation, current UTC offset, and daylight saving time flag.

quarter

Returns the quarter of the year, from 1 to 4, for a date or timestamp.

second

Returns the seconds part of a time, timestamp, or interval, without the fraction of a second.

strftime

Formats a date or timestamp as a string by using format specifiers such as %Y and %m.

strptime

Parses a string into a timestamp by using a format string, and fails when the string does not match.

time_bucket

Rounds a date, time, or timestamp down to the start of a fixed-width interval, such as 15 minutes or 1 week.

timezone

Converts between an instant and the local time in a time zone, or returns the UTC offset of a value in seconds.

timezone_hour

Returns the hours part of the UTC offset of a time or timestamp.

timezone_minute

Returns the minutes part of the UTC offset of a time or timestamp.

to_centuries

Returns an interval of a given number of centuries.

to_days

Returns an interval of a given number of days.

to_decades

Returns an interval of a given number of decades.

to_hours

Returns an interval of a given number of hours.

to_microseconds

Returns an interval of a given number of microseconds.

to_millennia

Returns an interval of a given number of millennia.

to_milliseconds

Returns an interval of a given number of milliseconds, which can include a fraction of a millisecond.

to_minutes

Returns an interval of a given number of minutes.

to_months

Returns an interval of a given number of months.

to_quarters

Returns an interval of a given number of quarters, where each quarter is three months.

to_seconds

Returns an interval of a given number of seconds, which can include a fraction of a second.

to_timestamp

Returns the TIMESTAMP WITH TIME ZONE value that is a given number of seconds after the Unix epoch.

to_weeks

Returns an interval of a given number of weeks.

to_years

Returns an interval of a given number of years.

today

Returns the current date in UTC.

transaction_timestamp

Alias of get_current_timestamp.

try_strptime

Parses a string into a timestamp by using a format string, and returns NULL when the string does not match.

week

Returns the ISO 8601 week number of a date or timestamp.

weekday

Alias of dayofweek.

weekofyear

Alias of week.

year

Returns the year of a date or timestamp, or the number of whole years in an interval.

yearweek

Returns the ISO 8601 year and week of a date or timestamp as one number in the form YYYYWW.