In this Python tutorial, I will show you exactly how to get the day of the week of a given date. For example, you want to find out the day’s name or number from the datetime. Show
In this datetime guide, I’ll cover:
Table of contentsHow to Get the day of the week from datetime in PythonThe below steps show how to use the datetime module’s Datetime is: 2022-04-13 11:21:18.052308 Day of a week is: 25 method to get the day of a week as an integer number.
Example: Get the day of a week from Datetime
Output: Datetime is: 2022-04-13 11:21:18.052308 Day of a week is: 2 The output is 2, equivalent to Wednesday as Monday is 0. Datetime is: 2022-04-13 11:21:18.052308 Day of a week is: 26 to get a weekday of a given date in PythonThe Datetime is: 2022-04-13 11:21:18.052308 Day of a week is: 25 method we used above returns the day of the week as an integer, where Monday is 0 and Sunday is 6. Use the method to get the day of the week as an integer, where Monday is 1 and Sunday is 7. i.e., To start from the weekday number from 1, we can use Datetime is: 2022-04-13 11:21:18.052308 Day of a week is: 26 in place of Datetime is: 2022-04-13 11:21:18.052308 Day of a week is: 25. Example:
Output: Datetime is: 2022-05-02 13:24:30.636135 Weekday is: 1 The output is 1, which is equivalent to Monday as Monday is 1. Get a weekday where Sunday is 0The strftime() approach If you’d like Sunday to be day 0 The Datetime is: 2022-04-13 11:21:18.052308 Day of a week is: 27 uses some standard directives to convert a datetime into a string format. The same directives are shared between 9 and Datetime is: 2022-04-13 11:21:18.052308 Day of a week is: 27 methods. The Datetime is: 2022-05-02 13:24:30.636135 Weekday is: 11 character code returns weekday as a decimal number, where 0 is Sunday, and 6 is Saturday.
Output: Date: 2022-05-02 1 Get the Weekday Name of a Date using strftime() methodIn the above examples, we saw how to get the weekday of a date as an integer. In this section, let’s see how to get the day name of a given date ( such as Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday) from a datetime object in Python. For example, Datetime is: 2022-05-02 13:24:30.636135 Weekday is: 12 should return me “Tuesday”. Use the strftime() method of a datetime module to get the day’s name in English in Python. It uses some standard directives to represent a datetime in a string format. The Datetime is: 2022-05-02 13:24:30.636135 Weekday is: 13 directive returns the full name of the weekday. Like, Monday, Tuesday. Example:
Output: Datetime is: 2022-05-02 13:40:57.060953 day Name: Monday Get the Weekday Name from date using Calendar ModulePython allows you to output calendars like the Unix cal program and provides additional useful functions related to the calendar. The Datetime is: 2022-05-02 13:24:30.636135 Weekday is: 14 method is used to get the name of the day from the date. This method contains an array that represents the days of the week in the current locale. Here, Monday is placed at index 0th index. Example:
Output: Date is: 2022-05-02 Weekday name is: Monday Check if a date is a weekday or weekendWe can use the Datetime is: 2022-04-13 11:21:18.052308 Day of a week is: 25 method of a Datetime is: 2022-05-02 13:24:30.636135 Weekday is: 16 object to determine if the given date is a weekday or weekend. Note: The weekday() method returns the day of the week as an integer, where Monday is 0 and Sunday is 6. For example, the date(2022, 05, 02) is a Monday. So its weekday number is 0. Example: Datetime is: 2022-04-13 11:21:18.052308 Day of a week is: 20 Output: Datetime is: 2022-04-13 11:21:18.052308 Day of a week is: 21 Pandas Timestamp Method to Get the Name of the Day in PythonIf you are working with a pandas series or dataframe, then the Datetime is: 2022-05-02 13:24:30.636135 Weekday is: 17 method is helpful to get the day number and name.
Example: Datetime is: 2022-04-13 11:21:18.052308 Day of a week is: 22 Output: Datetime is: 2022-04-13 11:21:18.052308 Day of a week is: 23 SummaryIn this tutorial, we learned how to get the Name of the day in English and the day of the week as an integer number in Python. |