Python weekday() Method Explained
In the datetime module of Python, the method weekday() is used to obtain the day of the week corresponding to a date object. This method returns an integer representing the day of the week, where 0 is Monday, 1 is Tuesday, and so on, with 6 representing Sunday.
Here is a basic usage example of the weekday() method:
import datetime
# 创建一个日期对象
date = datetime.datetime(2022, 9, 21) # 假设今天是2022年9月21日
# 使用 weekday() 方法获取日期对应的星期几(0代表星期一,6代表星期日)
weekday = date.weekday()
print(weekday)
In this example, if we assume 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).
Using the weekday() method allows for easily extracting information about which day of the week is associated with a date object, enabling further processing or decision-making based on this information.