How justify_hours() Works in PostgreSQL

In PostgreSQL, the justify_hours() function allows you to adjust an interval value, so that 24-hour time periods are represented as days.

For example, if you have an interval of say, 24 hours justify_hours() will return it as 1 day.

Syntax

The function has the following syntax:

justify_hours(interval)

Where interval is the interval that you want represented in days.

Example

Here’s a basic example to demonstrate.

SELECT justify_hours(interval '24 hours');

Result:

1 day

Partial Days

Here’s an example of what happens when your interval isn’t an exact multiple of 24 hours

\x
SELECT 
  justify_hours(interval '30 hours'),
  justify_hours(interval '300 hours'),
  justify_hours(interval '3000 hours'),
  justify_hours(interval '3.53 months');

Result (using vertical output):

justify_hours | 1 day 06:00:00
justify_hours | 12 days 12:00:00
justify_hours | 125 days
justify_hours | 3 mons 15 days 21:36:00

Notice that when providing the interval in months, the result is represented in terms of months and days, rather than just days.

In this example I used \x to switch over to expanded display, which displays the results using vertical output. This makes it slightly easier to read the results.

Less than a Day

If the interval is less than 24 hours, then it remains in hours.

SELECT justify_hours(interval '15 hours');

Result (using vertical output):

15:00:00