Home > Blockchain >  How do I calculate the difference between two timestamps?
How do I calculate the difference between two timestamps?

Time:10-04

How to calculate the difference in minutes between timestamps TIMEIN and TIMEOUT? My table (only 3 rows):

DATE TIMEIN TIMEOUT
2020-05-06 14:00 15:00
2020-05-06 14:45 15:55
2020-05-07 09:00 10:45

My SQL doesn't output what I want:

SELECT (T.DATE   T.TIMEIN - T.DATE   T.TIMEOUT) AS `Duration`

FROM Transport T;

Output:

Duration
29
29
19

It should be :

Duration
60
70
105

I tried this without using the date, however that lead to an output of -1 for all rows.

CodePudding user response:

Assuming that TIMEOUT is always greater than TIMEIN you can subtract the unix epochs of the 2 values and divide by 60 to get the number of minutes:

SELECT (strftime('%s', TIMEOUT) - strftime('%s', TIMEIN)) / 60 AS Duration
FROM Transport;

See the demo.

CodePudding user response:

date/time is not like integers so they didn't add/subtract like integers. So convert them in a timestamp before subtraction.

  • Related