Skip to content

toUnixTime - Represent a timestamp as time units since Epoch

toUnixTime converts a timestamp to a number of specific time units since the UNIX epoch (in UTC). The UNIX epoch starts on January 1, 1970.

Note

Timestamps from before Epoch are represented by negative numbers.

Syntax

Like many functions in DataPrime, toUnixTime supports two notations, function and method notation. These interchangeable forms allow flexibility in how you structure expressions.

toUnixTime(timestamp: timestamp, timeUnit: timeunit?): number
timestamp: timestamp.toUnixTime(timeUnit: timeunit?): number

Arguments

NameTypeRequiredDescription
timestamptimestamptrueThe timestamp to be converted to a unix timestamp
timeUnittimeunitfalseThe desired timeunit of the unix timestamp. Defaults to milli

Example - Changing the unix timestamp for compatability

Consider the following document:

{
    "ts": 1728769618374000000
}

Most systems expect the Unix timestamp to be in seconds. We can convert this using the toUnixTime function:

create ts_seconds from toUnixTime(ts, 's')
create ts_seconds from ts.toUnixTime('s')

This results in the following document:

{
    "ts": 1728769618374000000,
    "ts_seconds": 1728769618
}