我想确定日期上的一天是否是星期一,然后打印的日期从星期五的日期,即星期一至3日。我正在尝试下面的代码。
def giveDate(date):
if datetime.today().weekday()==0:
print("Monday")
date=date-3
print(date)
giveDate("2022-08-15")在giveDate中,如果datetime.today().weekday()==0: AttributeError:模块'datetime‘没有属性“今天”,我将得到以下错误行39
发布于 2022-08-15 12:36:29
这就是你想要的吗?
from datetime import datetime, timedelta
def giveDate(date: str) -> None:
# Convert given date string to datetime
date = datetime.strptime(date, '%Y-%m-%d')
if datetime.now().weekday() == 0:
print("Monday")
# Subtract 3 days and return date formatted similarly to input
date = (date - timedelta(days=3)).strftime('%Y-%m-%d')
print(date)giveDate("2022-08-15")
# OUT
# Monday
# 2022-08-12https://stackoverflow.com/questions/73352191
复制相似问题