

# Epoch conversion functions

Use these functions to convert between a date or time value and a count of seconds, milliseconds, microseconds, or nanoseconds since 1970-01-01 00:00:00 UTC, the Unix epoch. You can also get the length of an interval in these units, or the Julian day number of a date.

## `epoch` function
<a name="athena-duckdb-functions-date-time-epoch"></a>

Returns the number of seconds from the Unix epoch, 1970-01-01 00:00:00 UTC, to `temporal`, as a `DOUBLE` whose fractional part holds the microseconds. A value before the epoch returns a negative number. A `TIMESTAMP` value without a time zone is read as UTC. For a `TIME` or `TIME WITH TIME ZONE` value, returns the number of seconds since midnight, and the offset of a `TIME WITH TIME ZONE` value is not applied. For an `INTERVAL` value, returns the length of the interval in seconds.

If `temporal` is `NULL`, the result is `NULL`. To get whole milliseconds, microseconds, or nanoseconds as a `BIGINT`, use `epoch_ms`, `epoch_us`, or `epoch_ns`. To convert a number of seconds back to a timestamp, use the [`to_timestamp` function](#athena-duckdb-functions-date-time-to-timestamp).

### Syntax
<a name="athena-duckdb-functions-date-time-epoch-syntax"></a>

```sql
epoch({{temporal}})
```

### Arguments
<a name="athena-duckdb-functions-date-time-epoch-arguments"></a>

The `epoch` function takes the following argument:

**{{temporal}}**  
The value to convert to seconds, as a `DATE`, `TIME`, `TIME WITH TIME ZONE`, `TIMESTAMP`, `TIMESTAMP WITH TIME ZONE`, or `INTERVAL` value. Nanosecond values such as `TIMESTAMP_NS` are accepted too, and the result keeps microsecond precision.

### Return type
<a name="athena-duckdb-functions-date-time-epoch-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-date-time-epoch-examples"></a>

The following SQL query returns the number of seconds since the epoch for a timestamp.

```sql
SELECT
    epoch(
        TIMESTAMP '2021-08-03 11:59:44.123456'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1627991984.123456` | 

The following SQL query passes 1 AM on January 1, 1970, at UTC offset \+01. In UTC, that time is midnight, which is the Unix epoch itself, so the result is `0.0`.

```sql
SELECT
    epoch(
        TIMESTAMPTZ '1970-01-01 01:00:00+01'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `0.0` | 

The following SQL query returns the number of seconds since midnight for a time of day.

```sql
SELECT epoch(TIME '01:30:00') AS result
```

The query returns the following result.


| result | 
| --- | 
| `5400.0` | 

The following SQL query returns the length of a one-month interval and a one-year interval in seconds. The month counts as 30 days, and the year counts as 365.25 days.

```sql
SELECT
    epoch(INTERVAL '1 month') AS one_month,
    epoch(INTERVAL '1 year') AS one_year
```

The query returns the following result.


| one\_month | one\_year | 
| --- | --- | 
| `2592000.0` | `31557600.0` | 

### Considerations
<a name="athena-duckdb-functions-date-time-epoch-considerations"></a>

In an `INTERVAL`, each full 12 months count as 365.25 days, whether the interval is written in years or in months, and each remaining month counts as 30 days. For example, `epoch(INTERVAL '12 months')` returns `31557600.0`, but `epoch(INTERVAL '11 months')` returns `28512000.0`, which is 330 days.

For an infinite date or timestamp, such as `TIMESTAMP 'infinity'`, the result is `NULL`.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-date-time-epoch-differences-from-engine-version-3"></a>

Athena engine version 3 uses `to_unixtime` to get the number of seconds since the epoch. DuckDB does not have `to_unixtime`, so use `epoch`.

## `epoch_ms` function
<a name="athena-duckdb-functions-date-time-epoch-ms"></a>

Returns the number of milliseconds from the Unix epoch, 1970-01-01 00:00:00 UTC, to the date, time, timestamp, or interval value `temporal`, as a `BIGINT`. For a `TIME` or `TIME WITH TIME ZONE` value, the count starts at midnight, and the offset of a `TIME WITH TIME ZONE` value is not applied. For an `INTERVAL` value, the result is the length of the interval, where a month counts as 30 days and a year counts as 360 days. This differs from `epoch`, which counts a year as 365.25 days.

With a `BIGINT` argument, `epoch_ms` converts the other way: it reads the number as milliseconds since the epoch and returns the matching `TIMESTAMP`. If the argument is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-date-time-epoch-ms-syntax"></a>

```sql
epoch_ms({{temporal}})
epoch_ms({{milliseconds}})
```

### Arguments
<a name="athena-duckdb-functions-date-time-epoch-ms-arguments"></a>

The `epoch_ms` function takes the following arguments:

**{{temporal}}**  
The value to convert to milliseconds, as a `DATE`, `TIME`, `TIME WITH TIME ZONE`, `TIMESTAMP`, `TIMESTAMP WITH TIME ZONE`, or `INTERVAL` value.

**{{milliseconds}}**  
The number of milliseconds since the epoch to convert to a `TIMESTAMP`, as a `BIGINT` value.

### Return type
<a name="athena-duckdb-functions-date-time-epoch-ms-return-type"></a>

`BIGINT` for a date, time, timestamp, or interval argument, and `TIMESTAMP` for a `BIGINT` argument.

### Examples
<a name="athena-duckdb-functions-date-time-epoch-ms-examples"></a>

The following SQL query returns the number of milliseconds since the epoch for a timestamp.

```sql
SELECT
    epoch_ms(
        TIMESTAMP '2021-08-03 11:59:44.123456'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1627991984123` | 

The following SQL query converts a number of milliseconds since the epoch to a `TIMESTAMP`.

```sql
SELECT epoch_ms(1627991984123) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2021-08-03 11:59:44.123` | 

The following SQL query passes a timestamp whose fraction of a second has four digits. The result is rounded to the nearest millisecond.

```sql
SELECT
    epoch_ms(
        TIMESTAMP '2021-08-03 11:59:44.1236'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1627991984124` | 

### Considerations
<a name="athena-duckdb-functions-date-time-epoch-ms-considerations"></a>

For a `TIMESTAMP` or `TIMESTAMP WITH TIME ZONE` value, the result is rounded to the nearest millisecond. For example, `epoch_ms(TIMESTAMP '2021-08-03 11:59:44.1236')` returns a number that ends in `124`, not `123`. For `TIME` and `INTERVAL` values, the extra digits are dropped instead.

`epoch_ms` does not work on `TIMESTAMP_NS` values, and the query fails. `epoch_us` and `epoch_ns` accept `TIMESTAMP_NS` values.

## `epoch_ns` function
<a name="athena-duckdb-functions-date-time-epoch-ns"></a>

Returns the number of nanoseconds from the Unix epoch, 1970-01-01 00:00:00 UTC, to `temporal`, as a `BIGINT`. For a `TIME` or `TIME WITH TIME ZONE` value, the count starts at midnight, and the offset of a `TIME WITH TIME ZONE` value is not applied. For an `INTERVAL` value, the result is the length of the interval, where a month counts as 30 days and a year counts as 360 days. This differs from `epoch`, which counts a year as 365.25 days. If `temporal` is `NULL`, the result is `NULL`.

A `TIMESTAMP` value has microsecond precision, so the last three digits of its result are always `000`. To keep nanoseconds, pass a `TIMESTAMP_NS` value. To convert a number of nanoseconds back to a timestamp, use the [`make_timestamp_ns` function](#athena-duckdb-functions-date-time-make-timestamp-ns).

### Syntax
<a name="athena-duckdb-functions-date-time-epoch-ns-syntax"></a>

```sql
epoch_ns({{temporal}})
```

### Arguments
<a name="athena-duckdb-functions-date-time-epoch-ns-arguments"></a>

The `epoch_ns` function takes the following argument:

**{{temporal}}**  
The value to convert to nanoseconds, as a `DATE`, `TIME`, `TIME_NS`, `TIME WITH TIME ZONE`, `TIMESTAMP`, `TIMESTAMP_NS`, `TIMESTAMP WITH TIME ZONE`, or `INTERVAL` value.

### Return type
<a name="athena-duckdb-functions-date-time-epoch-ns-return-type"></a>

`BIGINT`

### Examples
<a name="athena-duckdb-functions-date-time-epoch-ns-examples"></a>

The following SQL query returns the number of nanoseconds since the epoch for a timestamp with microsecond precision.

```sql
SELECT
    epoch_ns(
        TIMESTAMP '2021-08-03 11:59:44.123456'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1627991984123456000` | 

The following SQL query passes a `TIMESTAMP_NS` value, so the result keeps all nine digits of the fraction of a second.

```sql
SELECT
    epoch_ns(
        TIMESTAMP_NS '2021-08-03 11:59:44.123456789'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1627991984123456789` | 

The following SQL query converts a timestamp to nanoseconds and back with `make_timestamp_ns`, and checks that the value is unchanged.

```sql
SELECT
    make_timestamp_ns(
        epoch_ns(
            TIMESTAMP_NS '2021-08-03 11:59:44.123456789'
        )
    ) = TIMESTAMP_NS '2021-08-03 11:59:44.123456789' AS is_round_trip
```

The query returns the following result.


| is\_round\_trip | 
| --- | 
| `true` | 

### Considerations
<a name="athena-duckdb-functions-date-time-epoch-ns-considerations"></a>

The result must fit in a `BIGINT`, so `epoch_ns` works only for times from 1677-09-21 00:12:43.145225 through 2262-04-11 23:47:16.854775 UTC. For a date or timestamp outside that range, the query fails.

`epoch_ns` does not accept a number. To convert nanoseconds since the epoch to a timestamp, use the [`make_timestamp_ns` function](#athena-duckdb-functions-date-time-make-timestamp-ns).

## `epoch_us` function
<a name="athena-duckdb-functions-date-time-epoch-us"></a>

Returns the number of microseconds from the Unix epoch, 1970-01-01 00:00:00 UTC, to `temporal`, as a `BIGINT`. For a `TIME` or `TIME WITH TIME ZONE` value, the count starts at midnight, and the offset of a `TIME WITH TIME ZONE` value is not applied. For an `INTERVAL` value, the result is the length of the interval, where a month counts as 30 days and a year counts as 360 days. This differs from `epoch`, which counts a year as 365.25 days. If `temporal` is `NULL`, the result is `NULL`.

For a `TIMESTAMP_NS` value, the nanosecond digits are dropped, not rounded. To convert a number of microseconds back to a `TIMESTAMP`, use the [`make_timestamp` function](duckdb-functions-date-time-construction.md#athena-duckdb-functions-date-time-make-timestamp) with one argument.

### Syntax
<a name="athena-duckdb-functions-date-time-epoch-us-syntax"></a>

```sql
epoch_us({{temporal}})
```

### Arguments
<a name="athena-duckdb-functions-date-time-epoch-us-arguments"></a>

The `epoch_us` function takes the following argument:

**{{temporal}}**  
The value to convert to microseconds, as a `DATE`, `TIME`, `TIME_NS`, `TIME WITH TIME ZONE`, `TIMESTAMP`, `TIMESTAMP_NS`, `TIMESTAMP WITH TIME ZONE`, or `INTERVAL` value.

### Return type
<a name="athena-duckdb-functions-date-time-epoch-us-return-type"></a>

`BIGINT`

### Examples
<a name="athena-duckdb-functions-date-time-epoch-us-examples"></a>

The following SQL query returns the number of microseconds since the epoch for a timestamp.

```sql
SELECT
    epoch_us(
        TIMESTAMP '2021-08-03 11:59:44.123456'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1627991984123456` | 

The following SQL query passes a `TIMESTAMP_NS` value. The last three digits of the fraction of a second are dropped.

```sql
SELECT
    epoch_us(
        TIMESTAMP_NS '2021-08-03 11:59:44.123456789'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1627991984123456` | 

The following SQL query converts a timestamp to microseconds and back with `make_timestamp`, and checks that the value is unchanged.

```sql
SELECT
    make_timestamp(
        epoch_us(TIMESTAMP '2021-08-03 11:59:44.123456')
    ) = TIMESTAMP '2021-08-03 11:59:44.123456' AS is_round_trip
```

The query returns the following result.


| is\_round\_trip | 
| --- | 
| `true` | 

### Considerations
<a name="athena-duckdb-functions-date-time-epoch-us-considerations"></a>

`epoch_us` does not accept a number. To convert microseconds since the epoch to a timestamp, use `make_timestamp({{microseconds}})`.

## `julian` function
<a name="athena-duckdb-functions-date-time-julian"></a>

Returns the Julian day number of `ts` as a `DOUBLE`. The Julian day number counts days from November 24, 4714 BC in the proleptic Gregorian calendar, so `DATE '1970-01-01'` is day `2440588`. For a `DATE`, the result is a whole number. For a timestamp, the fractional part is the part of the day that has passed since midnight, so 6 PM adds `0.75`.

A `TIMESTAMP WITH TIME ZONE` value is read in the session time zone, which is UTC. If `ts` is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-date-time-julian-syntax"></a>

```sql
julian({{ts}})
```

### Arguments
<a name="athena-duckdb-functions-date-time-julian-arguments"></a>

The `julian` function takes the following argument:

**{{ts}}**  
The value to convert to a Julian day number, as a `DATE`, `TIMESTAMP`, or `TIMESTAMP WITH TIME ZONE` value.

### Return type
<a name="athena-duckdb-functions-date-time-julian-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-date-time-julian-examples"></a>

The following SQL query returns the Julian day number of noon on January 1, 2006.

```sql
SELECT julian(TIMESTAMP '2006-01-01 12:00') AS result
```

The query returns the following result.


| result | 
| --- | 
| `2453737.5` | 

The following SQL query compares a date with a timestamp at 6 PM on the same day. The date gives a whole number, and the timestamp adds the fraction of the day.

```sql
SELECT
    julian(DATE '2006-01-01') AS from_date,
    julian(
        TIMESTAMP '2006-01-01 18:00:00'
    ) AS from_timestamp
```

The query returns the following result.


| from\_date | from\_timestamp | 
| --- | --- | 
| `2453737.0` | `2453737.75` | 

### Considerations
<a name="athena-duckdb-functions-date-time-julian-considerations"></a>

The fractional part counts from midnight, not from noon, so the result is `0.5` greater than the astronomical Julian date, which starts each day at noon UTC. For example, `julian(TIMESTAMP '2000-01-01 12:00:00')` returns `2451545.5`, and the astronomical Julian date of that moment is `2451545.0`. To get the astronomical Julian date, subtract `0.5`.

## `make_timestamp_ms` function
<a name="athena-duckdb-functions-date-time-make-timestamp-ms"></a>

Returns the `TIMESTAMP` that is `milliseconds` milliseconds after 1970-01-01 00:00:00. A negative value gives a timestamp before 1970. If `milliseconds` is `NULL`, the result is `NULL`.

`make_timestamp_ms({{milliseconds}})` returns the same value as `epoch_ms({{milliseconds}})`. For a number of microseconds, use `make_timestamp({{microseconds}})`, and for nanoseconds, use the [`make_timestamp_ns` function](#athena-duckdb-functions-date-time-make-timestamp-ns).

### Syntax
<a name="athena-duckdb-functions-date-time-make-timestamp-ms-syntax"></a>

```sql
make_timestamp_ms({{milliseconds}})
```

### Arguments
<a name="athena-duckdb-functions-date-time-make-timestamp-ms-arguments"></a>

The `make_timestamp_ms` function takes the following argument:

**{{milliseconds}}**  
The number of milliseconds since 1970-01-01 00:00:00, as a `BIGINT` value.

### Return type
<a name="athena-duckdb-functions-date-time-make-timestamp-ms-return-type"></a>

`TIMESTAMP`

### Examples
<a name="athena-duckdb-functions-date-time-make-timestamp-ms-examples"></a>

The following SQL query converts a number of milliseconds to a timestamp.

```sql
SELECT make_timestamp_ms(1732117793123) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2024-11-20 15:49:53.123` | 

The following SQL query passes a number of microseconds instead of milliseconds. The query succeeds, but the year of the result is far in the future.

```sql
SELECT
    year(make_timestamp_ms(1732117793000000)) AS result
```

The query returns the following result.


| result | 
| --- | 
| `56858` | 

### Considerations
<a name="athena-duckdb-functions-date-time-make-timestamp-ms-considerations"></a>

The argument is in milliseconds. If you pass a number of microseconds by mistake, the function does not fail: it returns a timestamp about 1,000 times farther from 1970, in a year such as `56858`.

## `make_timestamp_ns` function
<a name="athena-duckdb-functions-date-time-make-timestamp-ns"></a>

Returns the `TIMESTAMP_NS` value that is `nanoseconds` nanoseconds after 1970-01-01 00:00:00. `TIMESTAMP_NS` keeps nanoseconds, while `TIMESTAMP` keeps only microseconds. If `nanoseconds` is `NULL`, the result is `NULL`.

For a number of microseconds, use `make_timestamp({{microseconds}})`, and for milliseconds, use the [`make_timestamp_ms` function](#athena-duckdb-functions-date-time-make-timestamp-ms).

### Syntax
<a name="athena-duckdb-functions-date-time-make-timestamp-ns-syntax"></a>

```sql
make_timestamp_ns({{nanoseconds}})
```

### Arguments
<a name="athena-duckdb-functions-date-time-make-timestamp-ns-arguments"></a>

The `make_timestamp_ns` function takes the following argument:

**{{nanoseconds}}**  
The number of nanoseconds since 1970-01-01 00:00:00, as a `BIGINT` value.

### Return type
<a name="athena-duckdb-functions-date-time-make-timestamp-ns-return-type"></a>

`TIMESTAMP_NS`

### Examples
<a name="athena-duckdb-functions-date-time-make-timestamp-ns-examples"></a>

The following SQL query converts a number of nanoseconds to a timestamp.

```sql
SELECT make_timestamp_ns(1732117793000000000) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2024-11-20 15:49:53` | 

The following SQL query converts a number of nanoseconds that is not a whole number of seconds, so the result has a fractional second.

```sql
SELECT make_timestamp_ns(1732117793123456789) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2024-11-20 15:49:53.123456789` | 

The following SQL query shows that the result keeps all nine digits of the fractional second.

```sql
SELECT
    nanosecond(
        make_timestamp_ns(1732117793123456789)
    ) % 1000000000 AS result
```

The query returns the following result.


| result | 
| --- | 
| `123456789` | 

### Considerations
<a name="athena-duckdb-functions-date-time-make-timestamp-ns-considerations"></a>

Because the argument is a `BIGINT`, the result is between 1677-09-21 and 2262-04-11.

Casting the result to `TIMESTAMP` rounds it to microseconds.

## `to_timestamp` function
<a name="athena-duckdb-functions-date-time-to-timestamp"></a>

Returns the point in time that is `seconds` seconds after the Unix epoch, 1970-01-01 00:00:00 UTC, as a `TIMESTAMP WITH TIME ZONE` value. A fractional part adds a fraction of a second, and a negative value gives a time before the epoch. If `seconds` is `NULL`, the result is `NULL`.

The result is shown in the session time zone, which is UTC, so it ends in `+00`.

### Syntax
<a name="athena-duckdb-functions-date-time-to-timestamp-syntax"></a>

```sql
to_timestamp({{seconds}})
```

### Arguments
<a name="athena-duckdb-functions-date-time-to-timestamp-arguments"></a>

The `to_timestamp` function takes the following argument:

**{{seconds}}**  
The number of seconds since the Unix epoch, as a `DOUBLE` value.

### Return type
<a name="athena-duckdb-functions-date-time-to-timestamp-return-type"></a>

`TIMESTAMP WITH TIME ZONE`

### Examples
<a name="athena-duckdb-functions-date-time-to-timestamp-examples"></a>

The following SQL query converts a number of seconds, with a fractional part, to a timestamp.

```sql
SELECT to_timestamp(1284352323.5) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2010-09-13 04:32:03.5+00` | 

The following SQL query casts the result to a `TIMESTAMP` without a time zone.

```sql
SELECT
    CAST(
        to_timestamp(1600000000) AS TIMESTAMP
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `2020-09-13 12:26:40` | 

The following SQL query shows what happens when you pass milliseconds instead of seconds. Dividing by 1000 gives the intended year.

```sql
SELECT
    year(
        to_timestamp(1700000000000)
    ) AS year_from_milliseconds,
    year(
        to_timestamp(1700000000000 / 1000)
    ) AS year_from_seconds
```

The query returns the following result.


| year\_from\_milliseconds | year\_from\_seconds | 
| --- | --- | 
| `55840` | `2023` | 

### Considerations
<a name="athena-duckdb-functions-date-time-to-timestamp-considerations"></a>

`to_timestamp` expects seconds. If you pass milliseconds, the result is a date tens of thousands of years in the future, and the query does not fail. Divide a millisecond value by 1000 first, or use the [`epoch_ms` function](#athena-duckdb-functions-date-time-epoch-ms), which takes milliseconds and returns a `TIMESTAMP`.

To get a `TIMESTAMP` without a time zone, cast the result, as in `CAST(to_timestamp({{seconds}}) AS TIMESTAMP)`. The cast uses the session time zone, so the result is the date and time in UTC.

`to_timestamp` does not take a format argument. To convert text in a known layout to a timestamp, use the [`strptime` function](duckdb-functions-date-time-formatting-and-parsing.md#athena-duckdb-functions-date-time-strptime) or the [`try_strptime` function](duckdb-functions-date-time-formatting-and-parsing.md#athena-duckdb-functions-date-time-try-strptime).

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-date-time-to-timestamp-differences-from-engine-version-3"></a>

In Athena engine version 3, `to_timestamp` parses a string with a format, as in `to_timestamp('2024-01-02', 'yyyy-mm-dd')`. In DuckDB, `to_timestamp` takes only a number of seconds, and a call with a format fails. To parse a string, use the [`strptime` function](duckdb-functions-date-time-formatting-and-parsing.md#athena-duckdb-functions-date-time-strptime) with its own format specifiers, for example `strptime('2024-01-02', '%Y-%m-%d')`.

DuckDB does not have `from_unixtime`. To replace `from_unixtime({{seconds}})` from Athena engine version 3, use `CAST(to_timestamp({{seconds}}) AS TIMESTAMP)`.