要将日期转换为周几,我们可以使用Python的`datetime`模块。以下是一个函数,它接受一个日期字符串(格式为“YYYY-MM-DD”)作为输入,并返回对应的星期几。
```python
from datetime import datetime
def date_to_weekday(date_str):
将字符串转换为datetime对象
date_obj = datetime.strptime(date_str, '%Y-%m-%d')
获取星期几(0是星期一,6是星期日)
weekday = date_obj.weekday()
将数字转换为星期名称
weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
return weekdays[weekday]
示例使用
date_input = "2023-04-14"
weekday_output = date_to_weekday(date_input)
print(f"The day of the week for {date_input