How can I get the day name [such as Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday] from a datetime object in Python?
Nội dung chính
- Alternative
- Use the weekday[] Method to Get the Name of the Day in Python
- Use the isoweekday[] Method to Get the Name of the Day in Python
- Use the calendar Module to Get the Name of the Day in Python
- Use the strftime[] Method to Get the Name of the Day in Python
- Use Pandas Timestamp Method to Get the Name of the Day in Python
- Related Article - Python DateTime
- How do I get the day only in Python?
- How do you get the day of the week from a date in Python?
- How do I print only date from datetime 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
- HowTo
- Python How-To's
- Get the Day of the Week in Python
Created: November-26, 2020 | Updated: October-17, 2021
- Use the
weekday[]
Method to Get the Name of the Day in Python - Use the
isoweekday[]
Method to Get the Name of the Day in Python - Use the
calendar
Module to Get the Name of the Day in Python - Use the
strftime[]
Method to Get the Name of the Day in Python - Use Pandas
Timestamp
Method to Get the Name of the Day in Python
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.
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.
An example code of this method is given below:
from datetime import datetime
print[datetime.today[].weekday[]]
Output:
1
Use the isoweekday[] Method to Get the Name of the Day in Python
The
isoweekday[]
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[]
.
Below is the code example.
from datetime import datetime
print[datetime.today[].isoweekday[]]
Output:
2
The output is 2, which is equivalent to Tuesday as Monday is 1.
Use the calendar Module to Get the Name of the Day in Python
When the name of the day is required in English in
Python, the calendar
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.
An example of using this method is given below:
from datetime import date
import calendar
curr_date = date.today[]
print[calendar.day_name[curr_date.weekday[]]]
Output:
Tuesday
Use the strftime[] Method to Get the Name of the Day in Python
The strftime[]
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.
An example code of this approach is as follows:
from datetime import datetime
print[datetime.today[].strftime['%A']]
Output:
Tuesday
Use Pandas Timestamp Method to Get the Name of the Day in Python
Pandas Timestamp
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.
An example code is given below:
import pandas as pd
temp = pd.Timestamp['2020-11-25']
print[temp.dayofweek, temp.day_name[]]
Output:
2 Wednesday
Related Article - Python DateTime
How do I get the day only in Python?
“extract day from datetime python” Code Answer's.
import datetime..
dt = datetime. datetime. today[].
year = dt. year..
month = dt. month..
day = dt. day..
How do you get the day of the week from a date in Python?
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.
How do I print only date from datetime in Python?
Convert datetime Object to Date Only String in Python [Example].
1] Import datetime Module & Create Example Data..
2] Example 1: Extract Only Date from datetime Object Using strftime[] Function..
3] Example 2: Extract Only Date from datetime Object Using %s Operator..