Copy as Markdown[Open in ChatGPT](https://chatgpt.com/?q=Read%20https%3A%2F%2Fdocs-docusaurus.kinsta.page%2Fdataprime%2Flanguage-reference%2Ffunctions-reference%2Ftime%2Ftotimeunit.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)[Open in Claude](https://claude.ai/new?q=Read%20https%3A%2F%2Fdocs-docusaurus.kinsta.page%2Fdataprime%2Flanguage-reference%2Ffunctions-reference%2Ftime%2Ftotimeunit.md%20and%20help%20me%20with%20my%20question%20about%20this%20Coralogix%20documentation%20page.)

# `toTimeUnit`

## Description

Returns an interval converted to a numeric value in the requested time unit.

Use `toTimeUnit` when you need an interval as a plain number — for example, to report a duration in seconds, plot it on a numeric chart, or compare it against a numeric threshold.

## Syntax

Like many functions in DataPrime, `toTimeUnit` supports<!-- --> [two notations](https://docs-docusaurus.kinsta.page/dataprime/language-reference/functions-reference/.md),<!-- --> **function** and **method**. These interchangeable forms allow flexibility in how you structure expressions.

* Function notation
* Method notation

```
toTimeUnit(interval: interval, timeUnit?: timeunit): number
```

```
(interval: interval).toTimeUnit(timeUnit?: timeunit): number
```

## Arguments

| Name       | Type       | Required  | Description                                 |
| ---------- | ---------- | --------- | ------------------------------------------- |
| `interval` | `interval` | **true**  | The interval to convert                     |
| `timeUnit` | `timeunit` | **false** | The unit for the result. Defaults to `nano` |

## Example 1

**Use case: Convert a literal interval to a numeric value**

Convert an interval expression directly into a number of the requested units. Useful as a building block inside larger expressions.

### Example query

* Function notation
* Method notation

```
choose toTimeUnit(2h30m, 'h') as hours
```

```
choose 2h30m.toTimeUnit('h') as hours
```

### Example output

```
{

  "hours": 2.5

}
```

## Example 2

**Use case: Report event age in seconds**

Compute how long ago an event occurred and convert it to a numeric value in seconds — suitable for thresholds, charts, or downstream systems that expect a plain number.

### Example query

* Function notation
* Method notation

```
choose toTimeUnit(now() - $m.timestamp, 's') as age_seconds
```

```
choose (now() - $m.timestamp).toTimeUnit('s') as age_seconds
```

### Example output

```
{

  "age_seconds": 300

}
```
