Home > OS >  Time difference between two timestamps in postgres using age is not rounding up
Time difference between two timestamps in postgres using age is not rounding up

Time:11-16

Trying to determine the time difference between two timestamp fields in postgres in HH24:MI:SS format, i.e.

date_started and date_completed

that have the following data:

date_started = 12/11/2021 09:11:00
date_completed = 12/11/2021 09:19:00

Using the following query:

select to_char(AGE(date_completed, date_started),'hh24:mi:ss') as "time_diff"
from my_table

returns the following value: 00:07:59

Notes: both these fields have a data type of: timestamp without timezone

My question is, why is this not actually returning 00:08:00 seeing that it is exactly, 8 minutes difference?

CodePudding user response:

Solved my issue using the following:

select to_char(AGE(DATE_TRUNC('second', date_completed::timestamp), DATE_TRUNC('second', date_started::timestamp)),'hh24:mi:ss') as "time_diff"
from my_table

Reference SO: Discard milliseconds part from timestamp

Just want to also acknowledge @Bohemian for their input in assisting me to solve this issue wrt microseconds.

  • Related