# Chalk SQL Reference: Date and Time Functions
source: https://docs.chalk.ai/chalksql-docs#section-date-and-time-functions

## `current_date()`
source: https://docs.chalk.ai/chalksql-docs#current_date

Returns the current date.

**Parameters**

None

## `date(...)`
source: https://docs.chalk.ai/chalksql-docs#date

Converts a `timestamp` or `string` to a date.

**Examples**

```sql
SELECT date('2024-01-02');
```

```text
2024-01-02
```

**Overloads**

#### `date(timestamp)`

Converts a `timestamp` to a date.

**Parameters**

- `timestamp`: `timestamp[us, tz=UTC]`
  The input `timestamp`.

#### `date(string)`

Converts a `string` to a date.

**Parameters**

- `string`: `large_string`
  The input `string`.

## `date_add(...)`
source: https://docs.chalk.ai/chalksql-docs#date_add

Adds a `duration` or time-`unit` interval to a `date` or `timestamp`.

**Examples**

```sql
SELECT date_add('day', 7, TIMESTAMP '2024-01-02 00:00:00');
```

```text
2024-01-09 00:00:00
```

**Overloads**

#### `date_add(unit, value, timestamp)`

Adds the specified `value` of `unit` to a `timestamp`.

**Parameters**

- `unit`: `large_string`
  The time `unit`, such as 'second', 'day', or 'month'.
- `value`: `int64`
  The input `value`.
- `timestamp`: `timestamp[us, tz=UTC]`
  The input `timestamp`.

#### `date_add(date, duration)`

`date` + `duration` addition

**Parameters**

- `date`: `date32[day]`
  The input `date` or timestamp.
- `duration`: `duration[us]`
  The input `duration`.

#### `date_add(date, duration)`

`date` + month-day-nano interval addition

**Parameters**

- `date`: `date32[day]`
  The input `date` or timestamp.
- `duration`: `month_day_nano_interval`
  The input `duration`.

#### `date_add(date, duration)`

`date` + `duration` addition

**Parameters**

- `date`: `date64[ms]`
  The input `date` or timestamp.
- `duration`: `duration[us]`
  The input `duration`.

#### `date_add(date, duration)`

`date` + month-day-nano interval addition

**Parameters**

- `date`: `date64[ms]`
  The input `date` or timestamp.
- `duration`: `month_day_nano_interval`
  The input `duration`.

## `date_bin(bucket_width_seconds, origin_seconds, timestamp)`
source: https://docs.chalk.ai/chalksql-docs#date_bin

Truncates a `timestamp` onto a fixed-width grid of ``bucket_width_seconds``, anchored at ``origin_seconds`` (seconds since the Unix epoch).

**Parameters**

- `bucket_width_seconds`: `int64`
- `origin_seconds`: `int64`
- `timestamp`: `timestamp[us, tz=UTC]`
  The input `timestamp`.

## `date_diff(unit, timestamp1, timestamp2)`
source: https://docs.chalk.ai/chalksql-docs#date_diff

Returns the difference between two timestamps in the specified `unit`.

**Parameters**

- `unit`: `large_string`
  The time `unit`, such as 'second', 'day', or 'month'.
- `timestamp1`: `timestamp[us, tz=UTC]`
  The first timestamp.
- `timestamp2`: `timestamp[us, tz=UTC]`
  The second timestamp.

## `date_format(timestamp, format)`
source: https://docs.chalk.ai/chalksql-docs#date_format

Formats a `timestamp` as a string using the specified `format`.

**Parameters**

- `timestamp`: `timestamp[us, tz=UTC]`
  The input `timestamp`.
- `format`: `large_string`
  The `format` string.

## `date_trunc(unit, x)`
source: https://docs.chalk.ai/chalksql-docs#date_trunc

Truncates a timestamp to the specified time `unit` (e.g., day, month, year).

**Parameters**

- `unit`: `large_string`
  The time `unit`, such as 'second', 'day', or 'month'.
- `x`: `timestamp[us, tz=UTC]`
  The input value.

## `day(date)`
source: https://docs.chalk.ai/chalksql-docs#day

Extracts the day of the month from a `date` or timestamp.

**Overloads**

#### `day(date)`

**Parameters**

- `date`: `date32[day]`
  The input `date` or timestamp.

#### `day(date)`

**Parameters**

- `date`: `date64[ms]`
  The input `date` or timestamp.

#### `day(date)`

**Parameters**

- `date`: `timestamp[us, tz=UTC]`
  The input `date` or timestamp.

## `day_of_month(date)`
source: https://docs.chalk.ai/chalksql-docs#day_of_month

Extracts the day of the month from a timestamp.

**Overloads**

#### `day_of_month(date)`

**Parameters**

- `date`: `date32[day]`
  The input `date` or timestamp.

#### `day_of_month(date)`

**Parameters**

- `date`: `date64[ms]`
  The input `date` or timestamp.

#### `day_of_month(date)`

**Parameters**

- `date`: `timestamp[us, tz=UTC]`
  The input `date` or timestamp.

## `day_of_week(date)`
source: https://docs.chalk.ai/chalksql-docs#day_of_week

Extracts the day of the week from a timestamp.

**Overloads**

#### `day_of_week(date)`

**Parameters**

- `date`: `date32[day]`
  The input `date` or timestamp.

#### `day_of_week(date)`

**Parameters**

- `date`: `date64[ms]`
  The input `date` or timestamp.

#### `day_of_week(date)`

**Parameters**

- `date`: `timestamp[us, tz=UTC]`
  The input `date` or timestamp.

## `day_of_year(date)`
source: https://docs.chalk.ai/chalksql-docs#day_of_year

Extracts the day of the month from a timestamp.

**Overloads**

#### `day_of_year(date)`

**Parameters**

- `date`: `date32[day]`
  The input `date` or timestamp.

#### `day_of_year(date)`

**Parameters**

- `date`: `date64[ms]`
  The input `date` or timestamp.

#### `day_of_year(date)`

**Parameters**

- `date`: `timestamp[us, tz=UTC]`
  The input `date` or timestamp.

## `days_in_month(date)`
source: https://docs.chalk.ai/chalksql-docs#days_in_month

Returns the number of days in the month of the given `date`.

**Overloads**

#### `days_in_month(date)`

**Parameters**

- `date`: `date32[day]`
  The input `date` or timestamp.

#### `days_in_month(date)`

**Parameters**

- `date`: `date64[ms]`
  The input `date` or timestamp.

#### `days_in_month(date)`

**Parameters**

- `date`: `timestamp[us, tz=UTC]`
  The input `date` or timestamp.

## `dow(date)`
source: https://docs.chalk.ai/chalksql-docs#dow

Extracts the day of the week from a `date` or timestamp.

**Overloads**

#### `dow(date)`

**Parameters**

- `date`: `date32[day]`
  The input `date` or timestamp.

#### `dow(date)`

**Parameters**

- `date`: `date64[ms]`
  The input `date` or timestamp.

#### `dow(date)`

**Parameters**

- `date`: `timestamp[us, tz=UTC]`
  The input `date` or timestamp.

## `doy(date)`
source: https://docs.chalk.ai/chalksql-docs#doy

Extracts the day of the year from a `date` or timestamp.

**Overloads**

#### `doy(date)`

**Parameters**

- `date`: `date32[day]`
  The input `date` or timestamp.

#### `doy(date)`

**Parameters**

- `date`: `date64[ms]`
  The input `date` or timestamp.

#### `doy(date)`

**Parameters**

- `date`: `timestamp[us, tz=UTC]`
  The input `date` or timestamp.

## `format_datetime(timestamp, format)`
source: https://docs.chalk.ai/chalksql-docs#format_datetime

Formats a datetime using a specified `format` string.

**Parameters**

- `timestamp`: `timestamp[us, tz=UTC]`
  The input `timestamp`.
- `format`: `large_string`
  The `format` string.

## `format_datetime_in_timezone(timestamp, format, timezone)`
source: https://docs.chalk.ai/chalksql-docs#format_datetime_in_timezone

Formats a UTC `timestamp` as a string using a Joda `format` pattern in the given IANA `timezone`.

**Parameters**

- `timestamp`: `timestamp[us, tz=UTC]`
  The input `timestamp`.
- `format`: `large_string`
  The `format` string.
- `timezone`: `large_string`
  The IANA `timezone` name, such as 'America/New_York'.

## `from_iso8601_date(string)`
source: https://docs.chalk.ai/chalksql-docs#from_iso8601_date

Parses an ISO 8601 date `string` into a date value.

**Parameters**

- `string`: `large_string`
  The input `string`.

## `from_iso8601_timestamp(string)`
source: https://docs.chalk.ai/chalksql-docs#from_iso8601_timestamp

Parses an ISO 8601 timestamp `string` into a datetime object.

**Parameters**

- `string`: `large_string`
  The input `string`.

## `from_unix_seconds(unixtime)`
source: https://docs.chalk.ai/chalksql-docs#from_unix_seconds

Alias for [from_unixtime](https://docs.chalk.ai/chalksql-docs#from_unixtime).

## `from_unixtime(unixtime, ...)`
source: https://docs.chalk.ai/chalksql-docs#from_unixtime

Converts a UNIX timestamp to a timestamp, optionally with a time zone offset.

**Examples**

```sql
SELECT from_unixtime(1700000000);
```

```text
2023-11-14 22:13:20 UTC
```

**Overloads**

#### `from_unixtime(unixtime, hours, minutes)`

Returns the UNIX timestamp `unixtime` as a timestamp with time zone using `hours` and `minutes` for the time zone offset.

**Parameters**

- `unixtime`: `double`
- `hours`: `int64`
  The number of `hours`.
- `minutes`: `int64`
  The number of `minutes`.

#### `from_unixtime(unixtime)`

Returns the UNIX timestamp `unixtime` as a timestamp with time zone.

**Parameters**

- `unixtime`: `double`

## `hour(timestamp)`
source: https://docs.chalk.ai/chalksql-docs#hour

Extracts the hour from a `timestamp`.

**Parameters**

- `timestamp`: `timestamp[us, tz=UTC]`
  The input `timestamp`.

## `hour_in_timezone(timestamp, timezone)`
source: https://docs.chalk.ai/chalksql-docs#hour_in_timezone

Extracts the hour from a `timestamp` on a given `timezone`.

**Parameters**

- `timestamp`: `timestamp[us, tz=UTC]`
  The input `timestamp`.
- `timezone`: `large_string`
  The IANA `timezone` name, such as 'America/New_York'.

## `is_business_day(date)`
source: https://docs.chalk.ai/chalksql-docs#is_business_day

Returns true if the `date` falls on a weekday (Monday-Friday).

**Overloads**

#### `is_business_day(date)`

**Parameters**

- `date`: `date32[day]`
  The input `date` or timestamp.

#### `is_business_day(date)`

**Parameters**

- `date`: `date64[ms]`
  The input `date` or timestamp.

#### `is_business_day(date)`

**Parameters**

- `date`: `timestamp[us, tz=UTC]`
  The input `date` or timestamp.

## `is_federal_holiday(date)`
source: https://docs.chalk.ai/chalksql-docs#is_federal_holiday

Returns True if the given timestamp is a federal holiday.

**Overloads**

#### `is_federal_holiday(date)`

**Parameters**

- `date`: `date32[day]`
  The input `date` or timestamp.

#### `is_federal_holiday(date)`

**Parameters**

- `date`: `date64[ms]`
  The input `date` or timestamp.

#### `is_federal_holiday(date)`

**Parameters**

- `date`: `timestamp[us, tz=UTC]`
  The input `date` or timestamp.

## `last_day_of_month(date)`
source: https://docs.chalk.ai/chalksql-docs#last_day_of_month

Returns the last day of the month for a given `date`.

**Parameters**

- `date`: `timestamp[us, tz=UTC]`
  The input `date` or timestamp.

## `make_date(year, month, day)`
source: https://docs.chalk.ai/chalksql-docs#make_date

Constructs a date from `year`, `month`, and `day` fields.

**Parameters**

- `year`: `int64`
- `month`: `int64`
- `day`: `int64`

## `millisecond(timestamp)`
source: https://docs.chalk.ai/chalksql-docs#millisecond

Extracts the millisecond from a `timestamp`.

**Parameters**

- `timestamp`: `timestamp[us, tz=UTC]`
  The input `timestamp`.

## `minute(timestamp)`
source: https://docs.chalk.ai/chalksql-docs#minute

Extracts the minute from a `timestamp`.

**Parameters**

- `timestamp`: `timestamp[us, tz=UTC]`
  The input `timestamp`.

## `month(date)`
source: https://docs.chalk.ai/chalksql-docs#month

Extracts the month from a timestamp.

**Overloads**

#### `month(date)`

**Parameters**

- `date`: `date32[day]`
  The input `date` or timestamp.

#### `month(date)`

**Parameters**

- `date`: `date64[ms]`
  The input `date` or timestamp.

#### `month(date)`

**Parameters**

- `date`: `timestamp[us, tz=UTC]`
  The input `date` or timestamp.

## `parse_datetime(string, format)`
source: https://docs.chalk.ai/chalksql-docs#parse_datetime

Converts an ISO 8601 `string` into a datetime.

**Parameters**

- `string`: `large_string`
  The input `string`.
- `format`: `large_string`
  The `format` string.

## `quarter(timestamp)`
source: https://docs.chalk.ai/chalksql-docs#quarter

Extracts the quarter of the year from a `timestamp`.

**Parameters**

- `timestamp`: `timestamp[us, tz=UTC]`
  The input `timestamp`.

## `second(timestamp)`
source: https://docs.chalk.ai/chalksql-docs#second

Extracts the second from a `timestamp`.

**Parameters**

- `timestamp`: `timestamp[us, tz=UTC]`
  The input `timestamp`.

## `to_days(days)`
source: https://docs.chalk.ai/chalksql-docs#to_days

Constructs a duration with the given number of `days`.

**Overloads**

#### `to_days(days)`

**Parameters**

- `days`: `int64`
  The number of `days`.

#### `to_days(days)`

**Parameters**

- `days`: `double`
  The number of `days`.

## `to_hours(hours)`
source: https://docs.chalk.ai/chalksql-docs#to_hours

Constructs a duration with the given number of `hours`.

**Overloads**

#### `to_hours(hours)`

**Parameters**

- `hours`: `int64`
  The number of `hours`.

#### `to_hours(hours)`

**Parameters**

- `hours`: `double`
  The number of `hours`.

## `to_iso8601(timestamp)`
source: https://docs.chalk.ai/chalksql-docs#to_iso8601

Converts a datetime to an ISO 8601 string format.

**Parameters**

- `timestamp`: `timestamp[us, tz=UTC]`
  The input `timestamp`.

**Examples**

```sql
SELECT to_iso8601(TIMESTAMP '2024-01-02 03:04:05');
```

```text
2024-01-02T03:04:05.000000Z
```

## `to_microseconds(...)`
source: https://docs.chalk.ai/chalksql-docs#to_microseconds

Converts a `duration` to `microseconds` or constructs a `duration` from `microseconds`.

**Overloads**

#### `to_microseconds(duration)`

Converts a `duration` to microseconds.

**Parameters**

- `duration`: `duration[us]`
  The input `duration`.

#### `to_microseconds(duration)`

Converts a `duration` to microseconds.

**Parameters**

- `duration`: `duration[us]`
  The input `duration`.

#### `to_microseconds(microseconds)`

Constructs a duration with the given number of `microseconds`.

**Parameters**

- `microseconds`: `int64`
  The number of `microseconds`.

#### `to_microseconds(duration)`

Converts a `duration` to microseconds.

**Parameters**

- `duration`: `duration[us]`
  The input `duration`.

#### `to_microseconds(microseconds)`

Constructs a duration with the given number of `microseconds`.

**Parameters**

- `microseconds`: `double`
  The number of `microseconds`.

## `to_milliseconds(...)`
source: https://docs.chalk.ai/chalksql-docs#to_milliseconds

Converts a `duration` to `milliseconds` or constructs a `duration` from `milliseconds`.

**Overloads**

#### `to_milliseconds(duration)`

Converts a `duration` to milliseconds.

**Parameters**

- `duration`: `duration[us]`
  The input `duration`.

#### `to_milliseconds(duration)`

Converts a `duration` to milliseconds.

**Parameters**

- `duration`: `duration[us]`
  The input `duration`.

#### `to_milliseconds(milliseconds)`

Constructs a duration with the given number of `milliseconds`.

**Parameters**

- `milliseconds`: `int64`
  The number of `milliseconds`.

#### `to_milliseconds(duration)`

Converts a `duration` to milliseconds.

**Parameters**

- `duration`: `duration[us]`
  The input `duration`.

#### `to_milliseconds(milliseconds)`

Constructs a duration with the given number of `milliseconds`.

**Parameters**

- `milliseconds`: `double`
  The number of `milliseconds`.

## `to_minutes(minutes)`
source: https://docs.chalk.ai/chalksql-docs#to_minutes

Constructs a duration with the given number of `minutes`.

**Overloads**

#### `to_minutes(minutes)`

**Parameters**

- `minutes`: `int64`
  The number of `minutes`.

#### `to_minutes(minutes)`

**Parameters**

- `minutes`: `double`
  The number of `minutes`.

## `to_months(months)`
source: https://docs.chalk.ai/chalksql-docs#to_months

Constructs an interval with the given number of `months`.

**Overloads**

#### `to_months(months)`

**Parameters**

- `months`: `int64`

#### `to_months(months)`

**Parameters**

- `months`: `double`

## `to_seconds(seconds)`
source: https://docs.chalk.ai/chalksql-docs#to_seconds

Constructs a duration with the given number of `seconds`.

**Overloads**

#### `to_seconds(seconds)`

**Parameters**

- `seconds`: `int64`
  The number of `seconds`.

#### `to_seconds(seconds)`

**Parameters**

- `seconds`: `double`
  The number of `seconds`.

## `to_unix_nanos(timestamp)`
source: https://docs.chalk.ai/chalksql-docs#to_unix_nanos

Converts a `timestamp` or date to nanoseconds since the Unix epoch (UTC). Throws if the value falls outside the int64 ns range (~1677 to ~2262).

**Overloads**

#### `to_unix_nanos(timestamp)`

**Parameters**

- `timestamp`: `date32[day]`
  The input `timestamp`.

#### `to_unix_nanos(timestamp)`

**Parameters**

- `timestamp`: `date64[ms]`
  The input `timestamp`.

#### `to_unix_nanos(timestamp)`

**Parameters**

- `timestamp`: `timestamp[us, tz=UTC]`
  The input `timestamp`.

## `to_unixtime(timestamp)`
source: https://docs.chalk.ai/chalksql-docs#to_unixtime

Converts a `timestamp` to Unix `timestamp` (seconds since epoch).

**Parameters**

- `timestamp`: `timestamp[us, tz=UTC]`
  The input `timestamp`.

## `to_years(years)`
source: https://docs.chalk.ai/chalksql-docs#to_years

Constructs an interval with the given number of `years`.

**Overloads**

#### `to_years(years)`

**Parameters**

- `years`: `int64`

#### `to_years(years)`

**Parameters**

- `years`: `double`

## `total_seconds(duration)`
source: https://docs.chalk.ai/chalksql-docs#total_seconds

Returns the length of the input `duration` in seconds.

**Parameters**

- `duration`: `duration[us]`
  The input `duration`.

## `week(date)`
source: https://docs.chalk.ai/chalksql-docs#week

Extracts the week of the year from a timestamp.

**Overloads**

#### `week(date)`

**Parameters**

- `date`: `date32[day]`
  The input `date` or timestamp.

#### `week(date)`

**Parameters**

- `date`: `date64[ms]`
  The input `date` or timestamp.

#### `week(date)`

**Parameters**

- `date`: `timestamp[us, tz=UTC]`
  The input `date` or timestamp.

## `week_of_year(date)`
source: https://docs.chalk.ai/chalksql-docs#week_of_year

Extracts the week of the year from a timestamp.

**Overloads**

#### `week_of_year(date)`

**Parameters**

- `date`: `date32[day]`
  The input `date` or timestamp.

#### `week_of_year(date)`

**Parameters**

- `date`: `date64[ms]`
  The input `date` or timestamp.

#### `week_of_year(date)`

**Parameters**

- `date`: `timestamp[us, tz=UTC]`
  The input `date` or timestamp.

## `year(date)`
source: https://docs.chalk.ai/chalksql-docs#year

Extracts the year from a timestamp.

**Overloads**

#### `year(date)`

**Parameters**

- `date`: `date32[day]`
  The input `date` or timestamp.

#### `year(date)`

**Parameters**

- `date`: `date64[ms]`
  The input `date` or timestamp.

#### `year(date)`

**Parameters**

- `date`: `timestamp[us, tz=UTC]`
  The input `date` or timestamp.

## `year_of_week(date)`
source: https://docs.chalk.ai/chalksql-docs#year_of_week

Extracts the year of the ISO week from a `date`.

**Overloads**

#### `year_of_week(date)`

**Parameters**

- `date`: `date32[day]`
  The input `date` or timestamp.

#### `year_of_week(date)`

**Parameters**

- `date`: `date64[ms]`
  The input `date` or timestamp.

#### `year_of_week(date)`

**Parameters**

- `date`: `timestamp[us, tz=UTC]`
  The input `date` or timestamp.

## `yow(date)`
source: https://docs.chalk.ai/chalksql-docs#yow

Extracts the year of the ISO week from a `date`.

**Overloads**

#### `yow(date)`

**Parameters**

- `date`: `date32[day]`
  The input `date` or timestamp.

#### `yow(date)`

**Parameters**

- `date`: `date64[ms]`
  The input `date` or timestamp.

#### `yow(date)`

**Parameters**

- `date`: `timestamp[us, tz=UTC]`
  The input `date` or timestamp.
