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
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:
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
:
This results in the following document:
Theme
Light