With code as simple as shown below, you can get the number of days, that have passed since epoch.
from time import time
secs = time()
day = 24 * 60 * 60
days = int(secs // day)
print(days)
But now I need to prevent all the Saturdays and Sundays from being included in the count. Step one would be to get the number of full weeks that have passed and multiply it by 5.
from time import time
secs = time()
week = 7 * 24 * 60 * 60
weekDays = int(secs // week)
weekDays *= 5
print(weekDays)
But I don't know, what to do next. Note that the task is to find the number of FULL weekdays that have passed, so on the 1000th weekday, the answer should be 999.
Also, if the answer on a given Friday is 2000, then it should increase to 2001 on Saturday because now we have an additional full Friday, that has passed. The answer should then remain the same on Sunday and Monday, but increase to 2002 on Tuesday because now we have an additional full Monday, that has passed.
I checked and the day on January 1st, 1970 (epoch date) was Thursday, so I suppose that has to be taken into account.
I hope I am able to explain my problem simply and fully. If not, please ask in the comments. Looking forward to your answers. Thanks in advance.
Iterate from start to end date and filter out saturdays and sundays:
from datetime import date, datetime, timedelta
def daterange(startDate, endDate, delta=timedelta(days=1)):
currentDate = startDate
while currentDate <= endDate:
# Exclude weekends!
if currentDate.isoweekday() < 6:
yield 1
currentDate += delta
_start = datetime(1995, 11, 5).date()
_end = datetime(1995, 11, 8).date()
print(f"Found {sum(daterange(_start, _end))} work days")
Out:
Found 3 work days