Python weekday() Function Explained

In Python, the function weekday() is not a standard library function. Perhaps what you are asking about is the datetime.weekday() method. This method returns an integer representing the day of the week for a given date (0 for Monday, 6 for Sunday).

Here is the basic usage of the weekday() method in the datetime module:

import datetime

# 创建一个日期对象
date = datetime.datetime(2022, 9, 21)  # 假设今天是2022年9月21日

# 使用 weekday() 方法获取日期对应的星期几(0代表星期一,6代表星期日)
weekday = date.weekday()

print(weekday)

In the example above, assuming today is September 21, 2022, the weekday() method will return 2 because September 21, 2022 falls on a Wednesday (0 represents Monday, 1 represents Tuesday, and so on).

bannerAds