ID : 376
viewed : 70
Tags : PythonPython DateTime
93
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.
weekday()
Method to Get the Name of the Day in PythonIn Python, weekday()
can be used to retrieve the day of the week. The datetime.today()
method returns the current date, and the 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
isoweekday()
Method to Get the Name of the Day in PythonThe 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.
calendar
Module to Get the Name of the Day in PythonWhen 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
strftime()
Method to Get the Name of the Day in PythonThe strftime()
method can also be used to get the name of the day in English in Python. The 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
Timestamp
Method to Get the Name of the Day in PythonPandas 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