Table of Contents
- 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
- 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
In Python, there are many situations where days and dates are involved. For example, to specify a particular time frame of a task, including dates in a data frame, etc. This tutorial will tell how day names can be found from a specific date mentioned in Python. There are various many ways to perform this.
Use the Calendar
Module to Get the Name of the Day in Python
The Calendar
Module is a built-in module in Python that is used for all the operations related to the calendar.
In this method, the calendar
module is used because of its day_name()
method. The day_name()
method is used to manage an array that consists of the days of the week. In the array, Monday
is marked at the 0th index value.
Example:
1 2 3 4 5 6 |
from datetime import date import calendar current_date = date.today() print(calendar.day_name[current_date.weekday()] |
Output:
Explanation:
- In the above code, we first start by importing the
datetime
module which is a module used in Python to work with dates and time. - Then we import the
calendar
module. - After that, we use the
date.today()
method to find out today’s date. - Finally, we use the
day_name()
method of thecalendar
module in which we use thedate.today()
method as an argument to find out the day.
Use the strftime()
Method to Get the Name of the Day in Python
The strftime()
method is used to return a string that represents date and time using a date, time or datetime object.
In this method, the strftime()
method takes the %A
which is a directive as a parameter that is used to return the name of the weekday.
Example:
1 2 3 4 |
from datetime import datetime print(datetime.today().strftime('%A')) |
Output:
Further reading:
Use Pandas Timestamp
Method to Get the Name of the Day in Python
Pandas
is one of the most common modules in python. This module helps in dealing with structured and time-series data in a more fast and efficient manner. This module is highly used in data analysis in python.
Pandas Timestamp
method is very useful if the date is specified in a string format. The Timestamp()
method accepts the date in YYYY-MM-DD
format as the parameter.
After that, the day_name()
method is used to return the name of the respective day.
Example:
1 2 3 4 5 |
import pandas as pd date = pd.Timestamp('2022-06-27') print(date.dayofweek, date.day_name()) |
Output:
Use the weekday()
Method to Get the Name of the Day in Python
The weekday()
method is a method of the datetime
module. This method is used to return the day of the week. The datetime.today()
method is used to return the current date while the weekday()
method returns the specific day of the week in the form of an integer.
In the weekday()
method, Monday
is indexed as 0
and Sunday
is indexed as 6.
Example:
1 2 3 4 |
from datetime import datetime print(datetime.today().weekday()) |
Output:
Use the isoweekday()
Method to Get the Name of the Day in Python
The isoweekday()
method is very similar to the weekday()
method. The one and only difference in this method is that Monday
is marked as 1
and not 0
like in the weekday()
method.
Example:
1 2 3 4 |
from datetime import datetime print(datetime.today().isoweekday()) |
Output:
That’s all about how to get day Name from date in Python.