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"
.
Georgy
10.7k7 gold badges62 silver badges68 bronze badges
asked Dec 5, 2011 at 2:27
0
import datetime
now = datetime.datetime.now[]
print[now.strftime["%A"]]
See the Python docs for datetime.now, datetime.strftime and more on strftime.
Hugo
26.1k7 gold badges80 silver badges95 bronze badges
answered Dec 5, 2011 at 2:29
Matt JoinerMatt Joiner
109k105 gold badges359 silver badges516 bronze badges
1
>>> from datetime import datetime as date
>>> date.today[].strftime["%A"]
'Monday'
answered Dec 5, 2011 at 2:36
AbhijitAbhijit
59.9k18 gold badges125 silver badges197 bronze badges
0
If you don't mind using another package, you can also use pandas to achieve what you want:
>>> my_date = datetime.datetime[2019, 9, 6, 11, 33, 0]
>>> pd.to_datetime[my_date].day_name[]
'Friday'
It does not sound like a good idea to use another package for such easy task, the advantage of it is that day_name
method seems more understandable to me than strftime["%A"]
[you might easily forget what is the right directive for the format to get the day
name].
Hopefully, this could be added to datetime package directly one day [e.g. my_date.day_name[]
].
answered Dec 8, 2021 at 8:10
NerxisNerxis
3,0082 gold badges18 silver badges35 bronze badges
import datetime
numdays = 7
base = datetime.date.today[]
date_list = [base + datetime.timedelta[days=x] for x in range[numdays]]
date_list_with_dayname = ["%s, %s" % [[base + datetime.timedelta[days=x]].strftime["%A"], base + datetime.timedelta[days=x]] for x in range[numdays]]
answered May 30, 2021 at 18:17
Alternative
You can use import time
instead of datetime
as following:
import time
WEEKDAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
now = time.localtime[]
weekday_index = now.tm_wday
print[WEEKDAYS[weekday_index]]
answered Feb 19 at 14:08
import datetime import pandas as pd data['day']=data.index
answered Jun 15 at 7:33
3
Created: November-26, 2020 | Updated: October-17, 2021 This tutorial introduces how to get the name of the day from a given date. There are multiple ways of getting the output, so the tutorial also describes a few different approaches to get the name of the day. In Python, An example code of this method is given below: Output: The Below is the code example. Output: The output is 2, which is equivalent to Tuesday as Monday is 1. When the name of the day is required in English in Python, the An example of using this method is given below: Output: The An example code of this approach is as follows: Output: Pandas An example code is given below: Output:weekday[]
Method to Get the Name of the Day in Pythonisoweekday[]
Method to Get the Name of the Day in Pythoncalendar
Module to Get the Name of the Day in Pythonstrftime[]
Method to Get the Name of the Day in PythonTimestamp
Method to Get the Name of the Day in PythonUse the
weekday[]
Method to Get the Name of the Day in Pythonweekday[]
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.from datetime import datetime
print[datetime.today[].weekday[]]
1
Use the
isoweekday[]
Method to Get the Name of the Day in Pythonisoweekday[]
method works similarly to the weekday[]
method. This
method is used when Monday is to be marked with 1
instead of 0
like in weekday[]
.from datetime import datetime
print[datetime.today[].isoweekday[]]
2
Use the
calendar
Module to Get the Name of the Day in Pythoncalendar
library can be used. It uses the day_name[]
method that manages
an array of the days of the week. In this array, Monday is placed at the 0th index.from datetime import date
import calendar
curr_date = date.today[]
print[calendar.day_name[curr_date.weekday[]]]
Tuesday
Use the
strftime[]
Method to Get the Name of the Day in Pythonstrftime[]
method can also be used to get the name of the day in English in Python. The
strftime[]
method takes %A
directive as its only parameter which returns the full name of the weekday.from datetime import datetime
print[datetime.today[].strftime['%A']]
Tuesday
Use Pandas
Timestamp
Method to Get the Name of the Day in PythonTimestamp
method is useful if you have the date in string format. It takes the date in YYYY-MM-DD
format as its parameter, and the day_name[]
method returns the name of the respective day.import pandas as pd
temp = pd.Timestamp['2020-11-25']
print[temp.dayofweek, temp.day_name[]]
2 Wednesday
Related Article - Python DateTime