Python Code To Get Day Of Week

Chapter: Python Last Updated: 22-09-2023 04:49:45 UTC

Program:

            /* ............... START ............... */
                

import datetime

# Create a datetime object for the desired date
date_string = "2023-09-22"  # Replace with your date
date_obj = datetime.datetime.strptime(date_string, "%Y-%m-%d")

# Get the day of the week as an integer (0 = Monday, 6 = Sunday)
day_of_week = date_obj.weekday()

# You can also get the day of the week as a string
day_name = date_obj.strftime("%A")

print(f"The day of the week for {date_string} is {day_of_week} ({day_name}).")


                /* ............... END ............... */
        

Output

The day of the week for 2023-09-22 is 4 (Friday).

In this example, September 22, 2023, is a Friday, so the program 
correctly outputs "4 (Friday)" as the day of the week.

Notes:

  • In this program we use the datetime.datetime.strptime() method to parse the date_string into a datetime object. The %Y-%m-%d format string specifies the expected format of the date string, where %Y represents the year with century as a decimal number, %m represents the month as a zero-padded decimal number, and %d represents the day of the month as a zero-padded decimal number.
  • date_obj.weekday() method to get the day of the week as an integer, where 0 represents Monday, 1 represents Tuesday, and so on, with 6 representing Sunday. The result is stored in the day_of_week variable.
  • date_obj.strftime("%A") method to get the day of the week as a string, where "%A" is a format code that returns the full weekday name. The result is stored in the day_name variable.
  • Finally, we print the result using an f-string, which allows us to insert the values of the variables into the string. The output includes the original date, the numeric day of the week, and the day name.
  • Example output for the date "2023-09-22":
Similar Programs Chapter Last Updated
Python Program To Check Whether Element Present In Set Or Not Example Python 04-10-2023
Python Program To Find Maximum And Minimum Number In A Set Python 04-10-2023
Python Program To Check Symmetric Matrix Python 04-10-2023
Python Program To Find Subsets Of A Set Python 04-10-2023
Python Program To Find Power Set Of A Set Python 04-10-2023
Remove All Duplicates From List Python Python 04-10-2023
Python Program To Find Symmetric Difference Of Two Sets Python 27-09-2023
Python Program To Find Common Item From Two Set Python 27-09-2023
Python Program To Get Unique Values From A List Python 27-09-2023
Python Encode And Decode String With Key Python 24-09-2023
Python Simple Encrypt Decrypt String Python 24-09-2023
Python Format String To Specific Length Python 24-09-2023
Python Code To Check If String Contains Substring Python 24-09-2023
Python Program To Find Most Repeated Word In A String Python 23-09-2023
Split String Into Words Python Python 23-09-2023
Remove All Punctuation Python Python 23-09-2023
Python Program To Reverse An Array Python 23-09-2023
Python Program To Find Number Of Palindrome In A String Python 23-09-2023
Python Program To Find Longest Common Substring Python 23-09-2023
Python Program To Find Number Of Days In A Given Month And Year Python 22-09-2023
Python Program To Calculate Age Of A Person Python 22-09-2023
Python Convert String To Date Without Time Python 22-09-2023
Python Program To Print Current Date And Time In Format dd/mm/yyyy Python 22-09-2023
Python Program To Find Working Days In A Month Python 19-09-2023
Python Code To Change Date Format Python 16-09-2023
Python Program To Calculate Number Of Days Between Two Dates Python 16-09-2023
Python Program To Calculate Age In Years Months And Days Python 16-09-2023
Python Program To Schedule A Job To Run After A Certain Amount Of Time Python 10-08-2023
Python Program To Schedule A Job To Run Randomly Once A Day Python 10-08-2023
Python Program To Schedule A Job To Run Every Hour Python 10-08-2023

1 2 3 4