Date Functions
Supported Date Functions
CURRENT_DATE
CURRENT_DATE()
Returns the current date
DATE_FROM_ISO_PARTS
DATE_FROM_ISO_PARTS(isoWeekYear, isoWeek, isoDayOfWeek, hour, second, minute, millisecond, timezone)
Constructs and returns a Date object given the date’s constituent ISO properties.
Example
DATE_FROM_PARTS
DATE_FROM_PARTS(year, month, day, hour, second, minute, millisecond, timezone)
Constructs and returns a Date object given the date’s constituent properties.
Example
DATE_FROM_STRING
DATE_FROM_STRING(expr, format, timezone, onError, onNull)
Converts a date/time string to a date object.
DATE_TO_PARTS
DATE_TO_PARTS(expr, timezone, iso8601)
Returns a document that contains the constituent parts of a given Date value.
Example
DATE_TO_STRING
DATE_TO_STRING(expr, format, timezone, onNull)
Converts a date object to a string according to a user-specified format.
Example
DAY_OF_MONTH
DAY_OF_MONTH(expr)
Returns the day of the month for a date as a number between 1 and 31.
DAY_OF_WEEK
DAY_OF_WEEK(expr)
Returns the day of the week for a date as a number between 1 (Sunday) and 7 (Saturday).
DAY_OF_YEAR
DAY_OF_YEAR(expr)
Returns the day of the year for a date as a number between 1 and 366.
HOUR
HOUR(expr)
Returns the hour portion of a date as a number between 0 and 23.
ISO_DAY_OF_WEEK
ISO_DAY_OF_WEEK(expr)
Returns the weekday number in ISO 8601 format, ranging from 1 (for Monday) to 7 (for Sunday).
ISO_WEEK
ISO_WEEK(expr)
Returns the week number in ISO 8601 format, ranging from 1 to 53. Week numbers start at 1 with the week (Monday through Sunday) that contains the year’s first Thursday.
ISO_WEEK_YEAR
ISO_WEEK_YEAR(expr)
Returns the year number in ISO 8601 format. The year starts with the Monday of week 1 and ends with the Sunday of the last week.
MILLISECOND
MILLISECOND(expr)
Returns the millisecond portion of a date as an integer between 0 and 999.
MINUTE
MINUTE(expr)
Returns the minute portion of a date as a number between 0 and 59.
MONTH
MONTH(expr)
Returns the month of a date as a number between 1 and 12.
SECOND
SECOND(expr)
Returns the second portion of a date as a number between 0 and 59, but can be 60 to account for leap seconds.
WEEK
WEEK(expr)
Returns the week of the year for a date as a number between 0 and 53.
YEAR
YEAR(expr)
Returns the year portion of a date.
EXTRACT
EXTRACT(period from expr)
Extracts a portion of the date as per Postgres standard. Supported time periods: year, month, day, hour, minute, second, milliseconds, week, dow
Example
DATE_ADD
DATE_ADD(date, unit, amount, [timezone])
Adds a specified time interval to a date.
Example DATE_ADD
usage
You can also use the alias DATEADD
:
DATE_SUBTRACT
DATE_SUBTRACT(date, unit, amount, [timezone])
Subtracts a specified time interval from a date.
Example DATE_SUBTRACT
usage
You can also use the alias DATESUBTRACT
:
DATE_DIFF
DATE_DIFF(startDate, endDate, unit, [timezone], [startOfWeek])
Calculates the difference between two dates in the specified unit.
Example DATE_DIFF
usage
You can also use the alias DATEDIFF
:
DATE_TRUNC
DATE_TRUNC(date, unit)
Truncates a date to the specified unit of granularity.
Example DATE_TRUNC
usage
The unit
parameter can be one of the following:
- 'year'
- 'quarter'
- 'month'
- 'week'
- 'day'
- 'hour'
- 'minute'
- 'second'
- 'millisecond'
This function is useful for grouping dates by a specific time unit or for finding the start of a time period.
Example DATE_TRUNC
usage in grouping
This query groups orders by month and counts the number of orders in each month.