How can I get the day name (such as Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday) from a datetime object in Python?
So, for example, datetime(2019, 9, 6, 11, 33, 0)
should give me "Friday"
.
Use the strftime() method of a datetime module to get the day's name in English in Python. It uses some standard directives to represent a datetime in a string format. The %A directive returns the full name of the weekday. Like, Monday, Tuesday.
Use the weekday() Method to Get the Name of the Day in Python. In Python, weekday() can be used to retrieve the day of the week. The datetime. today() method returns the current date, and the weekday() method returns the day of the week as an integer where Monday is indexed as 0 and Sunday is 6.
Alternatively using from time import time : time() // (24 * 60 * 60) .
You can use index number like this: days=["sunday","monday"," Tuesday", "Wednesday" ,"Thursday", "Friday", "Saturday"] def date(i): return days[i] print (date(int(input("input index. "))))
import datetime now = datetime.datetime.now() print(now.strftime("%A"))
See the Python docs for datetime.now, datetime.strftime and more on strftime.
>>> from datetime import datetime as date >>> date.today().strftime("%A") 'Monday'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With