When working with dates, it often becomes necessary to determine the corresponding day of the week. Python's datetime module provides a convenient method to accomplish this task.
To retrieve the day of the week from a datetime object, utilize the weekday() method. This method returns an integer representing the day of the week, with Monday being 0 and Sunday being 6.
>>> today = datetime.datetime(2017, 10, 20) >>> today.weekday() 6
In this example, the output is 6, indicating that October 20, 2017, falls on a Friday.
The datetime.datetime.today() method provides access to the current date and time. By combining this with weekday(), you can easily determine the day of the week for today:
>>> import datetime >>> datetime.datetime.today() datetime.datetime(2023, 3, 7, 15, 37, 51, 777063) >>> datetime.datetime.today().weekday() 2
In this case, the output is 2, confirming that March 7, 2023, is a Tuesday.
The above is the detailed content of How Can Python's `datetime` Module Help Determine a Date's Day of the Week?. For more information, please follow other related articles on the PHP Chinese website!