Skip to content

toInterval - Interpret numbers as intervals

The toInterval function will take a given number, and a time unit, and convert it to an interval. This is very useful when we're attempting to perform some calculations on a date, but we do not have our fields in the correct types.

toInterval works with both integer and decimal values. It also works with positive and negative values.

Syntax

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

toInterval(number: number, timeUnit: timeunit?): interval
number: number.toInterval(timeUnit: timeunit?): interval

Arguments

NameTypeRequiredDescription
numbernumbertrueThe number to be converted to an interval
timeUnittimeunitfalseThe time unit for the number. Defaults to nano

Example - Add numeric duration to a timestamp to compute end time.

Consider the following document:

{
    "start_time": 1728763337,
    "duration_seconds": 50
}

If we want to calculate end_time, we can first use toInterval to convert the duration_seconds field into an actual interval, before we add it to the start_time:

create end_time from addTime(start_time, duration_seconds.toInterval('s'))
create end_time from start_time.addTime(duration_seconds.toInterval('s'))

This results in the following document:

{
    "start_time": 1728763337,
    "end_time": 1728763387
    "duration_seconds": 50
}