Date and Time Functions and Operators¶
This page documents date and time functions available in Polars SQL.
Date and Time Functions¶
- current_date -> date()¶
Returns the current date.
- current_timestamp -> timestamp()¶
Returns the current timestamp.
Interval Functions¶
The supported date_add form accepts these units:
dateinput:day,month,yeartimestampinput:day,hour,minute,second,month,year
- date_add(unit, value, timestamp) -> [same as input]()¶
Adds an interval
valueof typeunittotimestamp. Subtraction can be performed by using a negative value.Verified examples:
SELECT date_add('day', 5, date '2024-01-10'); -- 2024-01-15 SELECT date_add('month', 1, date '2024-01-31'); -- 2024-02-29 SELECT date_add('hour', 2, timestamp '2024-01-01 01:00:00'); -- 2024-01-01 03:00:00.0
- dateadd(unit, value, timestamp) -> [same as input]()¶
Alias for
date_add().
- date_add(date_expr, delta) -> [same as input]()
Adds
deltadays todate_expr.
- date_diff(timestamp1, timestamp2) -> integer()¶
Returns
timestamp2 - timestamp1in days.
- date_diff(timestamp1, timestamp2, unit) -> integer()
Returns
timestamp2 - timestamp1in the specifiedunit. Supported units includeyear,month,day,hour,minuteandsecond.
- datedif(timestamp1, timestamp2[, unit]) -> integer()¶
Alias for
date_diff().
Extraction Function¶
The extract function supports the following fields:
Field |
Description |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- extract(field FROM x) -> integer()¶
Returns
fieldfromx.Note
This SQL-standard function uses special syntax for specifying the arguments.
Convenience Extraction Functions¶
- day(x) -> integer()¶
Returns the day of the month from
x.
- hour(x) -> integer()¶
Returns the hour of the day from
x. The value ranges from0to23.
- minute(x) -> integer()¶
Returns the minute of the hour from
x.
- month(x) -> integer()¶
Returns the month of the year from
x.
- quarter(x) -> integer()¶
Returns the quarter of the year from
x. The value ranges from1to4.
- second(x) -> integer()¶
Returns the second of the minute from
x.
- year(x) -> integer()¶
Returns the year from
x.