基本上,我试图检查一个日期,例如2021-07-08,是在下一周,还是在下一周之后,或者两者都不在
#I can call the start and end dates of the current week
start = tday - timedelta(days=tday.weekday())
end = start + timedelta(days=6)
print("Today: " + str(tday))
print("Start: " + str(start))
print("End: " + str(end))
# and I can get the current week number.
curr_week = datetime.date.today().strftime("%V")
print(curr_week)
有没有比在curr_week+1中获取日期列表,然后检查该列表中是否有日期更好的方法? 非常感谢
[参见阿尔弗雷德的答案]
您可以直接从每个日期的
IsoCalendarDate
表示中获取整数形式的周数您可以在datetime中强制转换要签入的日期,然后比较周数
一般答复
最好坚持使用datetime和timedelta,因为这可以处理所有的边缘情况,如年份变化、有53周的年份等
因此,找出下周的数字,并将你想要检查的周数与之进行比较
旧答案
这会把年份的变化搞得一团糟,而模运算并不能解决这个问题,因为有53周的年份
您可以通过将周数转换为整数来比较周数。您不需要创建下周内所有日期的列表
相关问题 更多 >
编程相关推荐