-1

Let's assume that I have a list of dates like:

["2022-01-02", "2022-01-03"]

and so on. I also have a list of dates that are considered as a "holiday" in the same format. Now, I want to check if the dates from the first list cover the whole business week (so Monday to Friday), while taking the holiday list to consideration. So, if for example, I get provided with a list of days from Tuesday to Friday and there's no holiday on Monday then I'll get a False. But, if there would be a holiday on Monday, then I'd get a True.

Would be perfect if it worked between any two given dates, so that it won't be week-specific.

Is there any nice way of doing such things in python? How would you do it? Thanks in advance

Jakub Sapko
  • 304
  • 2
  • 15
  • 1
    The datetime module will be helpful for you. https://stackoverflow.com/questions/9847213/how-do-i-get-the-day-of-week-given-a-date/9847269#9847269 gives already an answer about how to identify the weekday of a certain date. – Flow Dec 09 '22 at 13:12

1 Answers1

0

Its a little difficult for me to exactly understand what you want, but I'll give you a head start and then you can tweak the if/else statements to your desire.

The datetime module has many features that might be of interest to you, including isofdateformat and isoweekday

The following code checks whether a day is a weekday and whether any of the days are holidays:

from datetime import date

# List of dates to check
dates = ["2022-01-04", "2022-01-05", "2022-01-06"]

# List of holidays
holidays = ["2022-01-03", "2022-01-06"]

# Convert the dates in the list to date objects
date_objects = [date.fromisoformat(d) for d in dates]

covers_week = True

for d in date_objects:
    if d.isoweekday() not in range(1, 6):
        # If any of the dates are not weekdays, the list does not cover a whole business week
        covers_week = False
        
# Check if there are any holidays in the list
for d in date_objects:
    if d in holidays:
        # If there are any holidays in the list, the list does not cover a whole business week
        covers_week = False
        
# Print result
print(covers_week)
Robin van Hoorn
  • 334
  • 1
  • 10