我发现这很困难,而且我不太确定要使用哪个导入。无论哪种方式:
from datetime import datetime
from datetime import timedelta
from datetime import date
import holidays
def isTradingDay(theDate):
isWeekday = False
isBankHoliday = False
# Select country
holidayDates = holidays.UnitedKingdom()
#Check WeekDay and Bank Holiday status
if(datetime.today().weekday() < 6):
isWeekday = True
if(theDate in holidayDates):
isBankHoliday = True
#Return True if trading day
if(isWeekday == False and isBankHoliday == False):
return True
else:
return False
问题出在datetime.today().weekday() < 6
上,我想使用此行检查值theDate
是否是工作日。但是我找不到检查theDate的正确方法。
theDate格式为:(datetime.today() - timedelta(days = offset)).strftime('%d-%m-%Y')
上面是否有可以使用的选项?
答案 0 :(得分:0)
如果theDate
是datetime
对象,则也可以使用.weekday()
函数:
if(theDate.weekday() < 6):
如果是字符串,则应首先使用strptime将其转换为日期时间:
dtime = datetime.strptime(theDate, '%Y-%m-%d')
'%Y-%m-%d'
字符串取决于您的theDate
字符串格式