---
title: time_diff function
description: Learn the syntax of the time\_diff function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/gcp/en/sql/language-manual/functions/time_diff
---

# `time_diff` function

**Applies to:** ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks Runtime 18 LTS and above

> [!NOTE]
>
> Databricks Runtime 18 is newer than Databricks Runtime 18.0, 18.1, and 18.2. Features that would previously have shipped as a later numbered version now ship as dated updates to Databricks Runtime 18 instead. For details, see [About unified release notes](https://docs.databricks.com/gcp/en/release-notes/runtime/18.md#unified-format).

Returns the difference between two time values measured in `unit`s.

## Syntax

```
time_diff(unit, start, end)

unit
 { MICROSECOND |
   MILLISECOND |
   SECOND |
   MINUTE |
   HOUR }
```

## Arguments

* `unit`: A unit of measure.
* `start`: A starting [TIME](https://docs.databricks.com/gcp/en/sql/language-manual/data-types/time-type.md) expression.
* `end`: An ending [TIME](https://docs.databricks.com/gcp/en/sql/language-manual/data-types/time-type.md) expression.

## Returns

A `BIGINT`.

If `start` is greater than `end` the result is negative.

## Examples

```sql
> SELECT time_diff(HOUR, TIME'08:00:00', TIME'21:30:00');
  13

> SELECT time_diff(MINUTE, TIME'08:00:00', TIME'08:45:30');
  45

> SELECT time_diff(SECOND, TIME'10:00:00', TIME'10:00:30');
  30

-- Start is greater than end
> SELECT time_diff(HOUR, TIME'21:00:00', TIME'08:00:00');
  -13
```

## Related functions

* [`date_diff` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/date_diff.md)
* [`timestampdiff` function](https://docs.databricks.com/gcp/en/sql/language-manual/functions/timestampdiff.md)
* [`TIME` type](https://docs.databricks.com/gcp/en/sql/language-manual/data-types/time-type.md)
