I have a table in PostgreSQL with two columns, created_on
and closed_on
, which contain timestamps in Unix milliseconds. I want to get the difference between these two timestamps in a readable format, such as "1 year, 9 days, 1 hour, 6 minutes, 1 second".
My initial table :
id | created_on | closed_on |
---|---|---|
1 | 1654850834025 | 1687168395586 |
I have tried the following query:
SELECT
(closed_on - created_on) * interval '1 millisecond' as interval1,
justify_interval(32317561561 * interval '1 millisecond') as interval2,
to_timestamp(closed_on / 1000)::timestamp - to_timestamp(created_on / 1000)::timestamp as interval3,
to_timestamp(created_on / 1000)::timestamp as created_on,
to_timestamp(closed_on / 1000)::timestamp as closed_on
FROM test
This query returns the following results:
interval1 | interval2 | interval3 | created_on | closed_on |
---|---|---|---|---|
{"hours":8977,"minutes":6,"seconds":1,"milliseconds":561} | {"years":1,"days":14,"hours":1,"minutes":6,"seconds":1,"milliseconds":561} | {"days":374,"hours":1,"minutes":6,"seconds":1} | 2022-06-10T08:47:14.000Z | 2023-06-19T09:53:15.000Z |
However, the results are not as expected:
- interval1 only shows the hours, not the days, months, etc.
- interval2 incorrectly shows 14 days, when there should be 9 days in a year.
- interval3 shows the correct number of days, but it does not show the
years or months.
Expected result:
1 year, 9 days, 1 hour, 6 minutes, 1 second
Fiddle : https://www.db-fiddle.com/f/xnXPVmtBEBDih83ZutBMZ4/0
2
Answers
According to https://www.postgresql.org/docs/current/functions-datetime.html,
justify_interval
counts with 30-day months andtimestamp
subtraction counts with 24-hour days. Without a calendar and concrete start/end timestamps, that’s the best you can do.It seems you are looking for the
age
function instead:(online demo)
Why would you ever divide the epoch value by 1000? That is entirely different timestamp.
Also using justify_interval() you are saying to treat months as 30 days each, then it is 1 year and 14 days.
DBfiddle demo