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

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

Arguments

Name Type Required Description
number number true The number to be converted to an interval
timeUnit timeunit false The 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'))

This results in the following document:

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