Skip to content

Instantly share code, notes, and snippets.

@ismael3s
Last active May 27, 2024 19:48
Show Gist options
  • Save ismael3s/f2bb275c81c5a45fcc887f000d4b98c9 to your computer and use it in GitHub Desktop.
Save ismael3s/f2bb275c81c5a45fcc887f000d4b98c9 to your computer and use it in GitHub Desktop.
Calculate Working Hours between two dates in Postgres
create or replace
function calculate_working_hours(start_time TIMESTAMPTZ,
end_time TIMESTAMPTZ)
returns interval as $$
declare
date record;
minutes interval := interval '0 minutes';
begin
for date in
select
*
from
generate_series(start_time,
end_time,
'1 minutes')
where
generate_series::time between interval '09:00:00' and interval '18:00:00'
and extract('dow'
from
generate_series) not in (0, 6)
loop
minutes := minutes + interval '1 minutes';
end loop;
return minutes;
end;
$$ language plpgsql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment