Python weekday() Function Guide
The weekday() function is used to determine which day of the week a specified date falls on. To use the weekday() function, you first need to import the datetime module, create a date object using the datetime.datetime object, and then call the weekday() function.
Here is an example code:
import datetime
# 创建一个日期对象
date = datetime.datetime(2022, 10, 31)
# 获取日期是一周中的第几天
weekday = date.weekday()
print(weekday)
In the example above, we created a date object ‘date’ that represents October 31, 2022, and then called the weekday() function to get the day of the week for that date as an integer. The result is then printed out. The weekday() function returns an integer where 0 represents Monday, 1 represents Tuesday, and so on, with 6 representing Sunday.