4
Friends,
I would like to know how the number of working days between two dates can be calculated in Python 3.
Thank you!
[]s
4
Friends,
I would like to know how the number of working days between two dates can be calculated in Python 3.
Thank you!
[]s
4
then, as it is in Diego’s answer, to know the number of days between two dates, just subtract two objects of the type date
and take the attribute days
. Now, regarding working days: - counting Saturdays and Sundays is quiet - the date has to check the .weekday
, and then you can do more or less math, at the risk of a mistake, or make a loop and count the days.
Tying a loop from date to date is a great opportunity to create a Python iterator: a keyword function setting yield
which can then be used directly in a:
def iterdates(date1, date2):
one_day = datetime.timedelta(days = 1)
current = date1
while current < date2:
yield current
current += one_day
And then you can do:
a = datetime.date(2018,1, 25)
b = datetime.date(2018, 2, 10)
for d in iterdates(a, b):
if d.weekday() not in (5, 6):
print (d, d.weekday())
Or, to count the days:
dias_de_semana = sum(1 for day in iterdates(a, b) if day.weekday not in (5,6))
(weekday 5 and 6 are Saturday and Sunday)
Holidays, it’s obvious that it’s complicated: either you find an API that responds to any holiday in the country you want in the world (it’s possible there’s something like this on google Calendar), or make a hard-coding of the national holidays and compare with them.
For example, you can make a dictionary:
feriados = {
"confraternização universal": date(2018, 1, 1),
"carnaval": date(2018, 2, 13),
...
}
and then use something similar to the previous expression:
dias_uteis = sum(1 for day in iterdates(a, b) if day.weekday not in (5,6) and day not in feriados.values())
Hello @jsbueno thanks for the explanation and sorry for the delay! Since I’m new to Python and this was a problem I tried to create on my own, I think my level of knowledge at this point is not allowing me to move forward, even with your help. Maybe I’m skipping a few steps hehe. Abs
If useful (Pun not intended): https://api.workingdays.org/1.2/api-documentation.php#non_working_days
3
I hope it’s still useful,
There is a library called workalendar, that makes what you’re looking for simple.
Just import and set the Brazilian calendar.
import datetime
from workalendar.america import Brazil
cal = Brazil()
cal.holidays(2019)
The function below returns the working day after entering the initial date and the desired days difference:
def data_dias_uteis(data_inicial, dias):
return cal.add_working_days(data_inicial, dias).strftime('%d/%m/%Y')
print data_dias_uteis(date(2019, 01, 26), 35)
15/03/2019
Great suggestion of library!
0
You can use this library.
from datetime import date
from = date(2008, 8, 18)
to = date(2008, 9, 26)
delta = from - to
print delta.days
Hello, Diego! Thanks for the post! However I would like to know on "working days" (i.e., disregarding Saturdays, Sundays and holidays) and not on busy days.
Browser other questions tagged python-3.x
You are not signed in. Login or sign up in order to post.
I found this good source for national holidays: http://www.anbima.com.br/feriados/feriados.asp
– Al Martins