Skip to main content
These functions and operators operate on date and time data types.

Date and time operators

Time zone conversion

The AT TIME ZONE operator sets the time zone of a timestamp:

Date and time functions

current_date

current_date Returns the current date as of the start of the query.

current_time

current_time Returns the current time with time zone as of the start of the query.

current_timestamp

current_timestamp Returns the current timestamp with time zone as of the start of the query, with 3 digits of subsecond precision,

current_timestamp()

current_timestamp(p) Returns the current timestamp with time zone as timestamp-with-time-zone-data-typeof the start of the query, withp` digits of subsecond precision:

current_timezone()

current_timezone() → varchar Returns the current time zone in the format defined by IANA (e.g., America/Los_Angeles) or as fixed offset from UTC (e.g., +08:35)

date()

date(x) → date This is an alias for CAST(x AS date).

last_day_of_month()

last_day_of_month(x) → date Returns the last day of the month.

from_iso8601_timestamp()

from_iso8601_timestamp(string) → timestamp(3) with time zone Parses the ISO 8601 formatted date string, optionally with time and time zone, into a timestamp(3) with time zone. The time defaults to 00:00:00.000, and the time zone defaults to the session time zone:

from_iso8601_timestamp_nanos()

from_iso8601_timestamp_nanos(string) → timestamp(9) with time zone Parses the ISO 8601 formatted date and time string. The time zone defaults to the session time zone:

from_iso8601_date()

from_iso8601_date(string) → date Parses the ISO 8601 formatted date string into a date. The date can be a calendar date, a week date using ISO week numbering, or year and day of year combined:

at_timezone()

at_timezone(timestamp, zone) → timestamp(p) with time zone Returns the timestamp specified in timestamp with the time zone converted from the session time zone to the time zone specified in zone with precision p. In the following example, the session time zone is set to America/New_York, which is three hours ahead of America/Los_Angeles:

with_timezone()

with_timezone(timestamp, zone) → timestamp(p) with time zone Returns the timestamp specified in timestamp with the time zone specified in zone with precision p.

from_unixtime()

from_unixtime(unixtime) → timestamp(3) with time zone Returns the UNIX timestamp unixtime as a timestamp with time zone. unixtime is the number of seconds since 1970-01-01 00:00:00 UTC.

from_unixtime()

from_unixtime(unixtime, zone) → timestamp(3) with time zone Returns the UNIX timestamp unixtime as a timestamp with time zone using zone for the time zone. unixtime is the number of seconds since 1970-01-01 00:00:00 UTC.

from_unixtime()

from_unixtime(unixtime, hours, minutes) → timestamp(3) with time zone Returns the UNIX timestamp unixtime as a timestamp with time zone using hours and minutes for the time zone offset. unixtime is the number of seconds since 1970-01-01 00:00:00 in double data type.

from_unixtime_nanos()

from_unixtime_nanos(unixtime) → timestamp(9) with time zone Returns the UNIX timestamp unixtime as a timestamp with time zone. unixtime is the number of nanoseconds since 1970-01-01 00:00:00.000000000 UTC:

now()

now() → timestamp(3) with time zone This is an alias for current_timestamp.

to_iso8601()

to_iso8601(x) → varchar Formats x as an ISO 8601 string. x can be date, timestamp, or timestamp with time zone.

to_milliseconds()

to_milliseconds(interval) → bigint Returns the day-to-second interval as milliseconds.

to_unixtime()

to_unixtime(timestamp) → double Returns timestamp as a UNIX timestamp.

Truncation function

The date_trunc function supports the following units: The above examples use the timestamp 2001-08-22 03:04:05.321 as the input.

date_trunc()

date_trunc(unit, x) → [same as input] Returns x truncated to unit:

Interval functions

The functions in this section support the following interval units:

date_add()

date_add(unit, value, timestamp) → same as input Adds an interval value of type unit to timestamp. Subtraction can be performed by using a negative value:

date_diff()

date_diff(unit, timestamp1, timestamp2) → bigint Returns timestamp2 - timestamp1 expressed in terms of unit:

Duration function

The parse_duration function supports the following units:

parse_duration()

parse_duration(string) → interval Parses string of format value unit into an interval, where value is fractional number of unit values:

human_readable_seconds()

human_readable_seconds(double) → varchar Formats the double value of seconds into a human readable string containing weeks, days, hours, minutes, and seconds:

MySQL date functions

The functions in this section use a format string that is compatible with the MySQL date_parse and str_to_date functions. The following table, based on the MySQL manual, describes the format specifiers: The following specifiers are not currently supported: %D %U %u %V %w %X date_format(timestamp, format) → varchar Formats timestamp as a string using format:
date_parse(string, format) → timestamp Parses string into a timestamp using format:

Java date functions

The functions in this section use a format string that is compatible with JodaTime’s DateTimeFormat pattern format.

format_datetime()

format_datetime(timestamp, format) → varchar Formats timestamp as a string using format.

parse_datetime()

parse_datetime(string, format) → timestamp Parses string into a timestamp with time zone using format.

Extraction function

The extract function supports the following fields: The types supported by the extract function vary depending on the field to be extracted. Most fields support all date and time types. extract(field FROM x) → bigint Returns field from x:

Convenience extraction functions

day()

day(x) → bigint Returns the day of the month from x.

day_of_month()

day_of_month(x) → bigint This is an alias for day.

day_of_week()

day_of_week(x) → bigint Returns the ISO day of the week from x. The value ranges from 1 (Monday) to 7 (Sunday).

day_of_year()

day_of_year(x) → bigint Returns the day of the year from x. The value ranges from 1 to 366.

dow()

dow(x) → bigint This is an alias for day_of_week.

doy()

doy(x) → bigint This is an alias for day_of_year.

hour()

hour(x) → bigint Returns the hour of the day from x. The value ranges from 0 to 23.

millisecond()

millisecond(x) → bigint Returns the millisecond of the second from x.

minute()

minute(x) → bigint Returns the minute of the hour from x.

month()

month(x) → bigint Returns the month of the year from x.

quarter()

quarter(x) → bigint Returns the quarter of the year from x. The value ranges from 1 to 4.

second()

second(x) → bigint Returns the second of the minute from x.

timezone_hour()

timezone_hour(timestamp) → bigint Returns the hour of the time zone offset from timestamp.

timezone_minute()

timezone_minute(timestamp) → bigint Returns the minute of the time zone offset from timestamp.

week()

week(x) → bigint Returns the ISO week of the year from x. The value ranges from 1 to 53.

week_of_year()

week_of_year(x) → bigint This is an alias for week.

year()

year(x) → bigint Returns the year from x.

year_of_week()

year_of_week(x) → bigint Returns the year of the ISO week from x.

yow()

yow(x) → bigint This is an alias for year_of_week.

Footnotes

  1. This specifier does not support 0 as a month or day.
  2. This specifier does not support 0 as a month or day.
  3. This specifier does not support 0 as a month or day.
  4. Timestamp is truncated to milliseconds.
  5. This specifier does not support 0 as a month or day.
  6. This specifier is not supported yet. Consider using day_of_week (it uses 1-7 instead of 0-6).
  7. When parsing, two-digit year format assumes range 1970 .. 2069, so “70” will result in year 1970 but “69” will produce 2069.