Function Reference

TIMEDIFF

Returns the difference between two time or datetime values.

MySQL 8.1
TIMEDIFF(datetime1, datetime2)

Parameters

datetime1DATETIME
Time or datetime to subtract datetime2 from
datetime2DATETIME
Value of the same type to subtract

Return value

A time (TIME), can be negative and longer than 24 hours, but not beyond 838:59:59; NULL if the arguments have different types

Examples

Difference within one day

MySQL 8.1
SELECT TIMEDIFF('2022-12-05 18:00:00', '2022-12-05 14:30:00') AS diff;
diff
03:30:00

Flight duration

MySQL 8.1
SELECT id,
	town_from,
	town_to,
	TIMEDIFF(time_in, time_out) AS duration
FROM Trip;
idtown_fromtown_toduration
1100RostovParis03:20:00
1101ParisRostov03:33:00
1123RostovVladivostok11:20:00
1124VladivostokRostov10:50:00
1145MoscowRostov01:48:00
1146RostovMoscow02:06:00
1181RostovMoscow01:49:00
1182MoscowRostov01:55:00
1187RostovMoscow01:57:00
1188MoscowRostov01:58:00

Negative difference

MySQL 8.1
SELECT TIMEDIFF('14:00:00', '18:00:00') AS diff;
diff
-04:00:00

Too large a difference is capped

MySQL 8.1
SELECT TIMEDIFF('2022-12-05 18:00:00', '2021-12-01 14:00:00') AS diff;
diff
838:59:59

Details

To get the difference as a number of seconds, wrap the result in TIME_TO_SEC or use TIMESTAMPDIFF.

See also