triggerDiscipline
triggerDiscipline

Reputation: 37

How do i use a list of holidays like this to find holidays in my day's list. this method gives me an error

import datetime

startDate = '2020-01-01'
start = datetime.datetime.strptime(startDate, '%Y-%m-%d')


holidays=[2020-1-5, 2020-1-11, 2020-1-13]

for dayNum in range(0,366):
    dayOfYear = start + datetime.timedelta(days=dayNum)

    if dayNum in holidays:
        print("HOLIDAY")

Upvotes: 0

Views: 38

Answers (2)

Shashikumar KL
Shashikumar KL

Reputation: 1085

import datetime
startDate = '2020-01-01'
start = datetime.datetime.strptime(startDate, '%Y-%m-%d')
holidays=["2020-01-01", "2020-01-03", "2020-01-05"]
for dayNum in range(0,366): 
    dayOfYear = start + datetime.timedelta(days=dayNum)
    if str(dayOfYear).split(" ")[0] in holidays:
        print(str(dayOfYear).split(" ")[0]+" is HOLIDAY")

try this

Upvotes: 0

Alexey
Alexey

Reputation: 178

Something like this should work

from datetime import datetime, timedelta

startDate = '2020-01-01'
start = datetime.strptime(startDate, '%Y-%m-%d')

holidays_str = ['2020-01-05', '2020-01-11', '2020-01-13']
holidays = [datetime.strptime(day, '%Y-%m-%d') for day in holidays_str]

for day in range(0, 366):
    day_of_year = start + timedelta(days=day)

    if day_of_year in holidays:
        print(datetime.strftime(day_of_year, '%Y-%m-%d'), "HOLIDAY")

Upvotes: 1

Related Questions