Get Day Name from Date 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:

Output:

Monday

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 the calendar module in which we use the date.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:

Output:

Monday

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:

Output:

0 Monday

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 0and Sunday is indexed as 6.

Example:

Output:

0

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:

Output:

1

That’s all about how to get day Name from date in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *