Skip to content

toUnixTime

Description

Returns the number of time units since the Unix epoch (1970-01-01T00:00:00Z) for a given timestamp.

Note

Timestamps before the epoch are represented as 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 convert
timeUnittimeunitfalseThe unit for the result. Defaults to milli

Example

Use case: Convert a nanosecond timestamp into seconds for system compatibility

A log contains a timestamp in nanoseconds. Convert it to seconds to match external system requirements.

{
"ts": 1728769618374000000
}
create ts_seconds from toUnixTime(ts, 's')
create ts_seconds from ts.toUnixTime('s')

Output

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