unixtime_nanoseconds_fromdatetime
This page explains how to use the unixtime_nanoseconds_fromdatetime function in APL.
unixtime_nanoseconds_fromdatetime converts a datetime value to a Unix timestamp that’s expressed in whole nanoseconds since 1970-01-01 00:00:00 UTC.
Use the function when you need a timestamp as a number with an explicit unit. For example, to send epoch nanoseconds to another system, to compare timestamps with numeric fields that store epoch nanoseconds, or to aggregate a timestamp as a number with max(unixtime_nanoseconds_fromdatetime(_time)).
unixtime_nanoseconds_fromdatetime is the inverse of unixtime_nanoseconds_todatetime.
Usage
Syntax
Parameters
| Name | Type | Description |
|---|---|---|
datetime | datetime | The datetime value to convert. Other types aren’t converted implicitly and return a compile error. |
Returns
A long value that represents the number of whole nanoseconds since the Unix epoch. Precision finer than one nanosecond is dropped, rounding down toward negative infinity, so datetime values before 1970 return negative numbers. If the input is null, the function returns null.
For example, print epoch_ns = tostring(unixtime_nanoseconds_fromdatetime(datetime(2024-01-15T10:30:45.123456789Z))) returns 1705314645123456789.
Use case example
The HTTP access logs store the timestamp as a datetime and you want each event’s time as epoch nanoseconds.
Query
Output
| _time | epoch_ns_text |
|---|---|
| 2026-12-02T07:37:03Z | 1796197023000000000 |
This query converts each event’s timestamp to nanoseconds since the Unix epoch. The example converts the result to a string with tostring because some JSON clients, including browsers, can’t represent every 19-digit integer exactly.
List of related functions
- unixtime_seconds_fromdatetime: Converts a
datetimevalue to a Unix timestamp in whole seconds. - unixtime_milliseconds_fromdatetime: Converts a
datetimevalue to a Unix timestamp in whole milliseconds. - unixtime_microseconds_fromdatetime: Converts a
datetimevalue to a Unix timestamp in whole microseconds. - unixtime_nanoseconds_todatetime: Converts a Unix timestamp expressed in whole nanoseconds to a
datetimevalue. This is the inverse ofunixtime_nanoseconds_fromdatetime.