# TIMEDIFF

> TIMEDIFF function in StackQL: the difference between two time values as a signed years, months, days and time string.

Source: https://stackql.io/language-spec/functions/datetime/timediff

Returns the difference between two time values as a string of the form `(+|-)YYYY-MM-DD HH:MM:SS.SSS`, the amount of time that must be added to the second value to reach the first.

See also:  
[[` SELECT `]](/language-spec/select) [[` JULIANDAY `]](/language-spec/functions/datetime/julianday) [[` UNIXEPOCH `]](/language-spec/functions/datetime/unixepoch)

* * * 

## Syntax

```sql
SELECT TIMEDIFF(time_value1, time_value2) FROM <multipartIdentifier>;
```

## Arguments

__*time_value1*__  
The later time value, in any format the date and time functions accept.

__*time_value2*__  
The earlier time value.

## Return Value(s)
Returns a string, negative when *time_value1* is earlier than *time_value2*.  The result is `NULL` for an invalid time value.

* * *

## Examples

### Differences between time values

The month part depends on the calendar, so the reversed difference is not the same magnitude:

```sql
SELECT TIMEDIFF('2024-03-01', '2024-01-15') AS a,
TIMEDIFF('2024-01-15', '2024-03-01') AS b,
TIMEDIFF('2024-01-01 12:00:00', '2024-01-01 09:30:15') AS c;
```

```
|--------------------------|--------------------------|--------------------------|
|            a             |            b             |            c             |
|--------------------------|--------------------------|--------------------------|
| +0000-01-15 00:00:00.000 | -0000-01-17 00:00:00.000 | +0000-00-00 02:29:45.000 |
|--------------------------|--------------------------|--------------------------|
```

### Age of a resource

The following query shows how long after creation each repository in a GitHub organization last received a push:

```sql
SELECT name, TIMEDIFF(pushed_at, created_at) AS active_for
FROM github.repos.repos
WHERE org = 'stackql-labs';
```

For more information, see [https://www.sqlite.org/lang_datefunc.html](https://www.sqlite.org/lang_datefunc.html)
