El3na
El3na

Reputation: 31

program which gives the day of the week of day of the month

I would like to write a program that gives the day of the week on the 30th of the month. I have a code in which I have to specify the date, what can I change in it to work specifically for the 30th of the month?

import datetime
date=str(input('(Date for example 30 03 2019):'))
day_name= ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday']
day = datetime.datetime.strptime(date, '%d %m %Y').weekday()
print(day_name[day])

Upvotes: 1

Views: 53

Answers (1)

Gabio
Gabio

Reputation: 9504

try this:

from datetime import datetime
year = int(input("Please enter year:"))
month = int(input("Please enter month:"))
try:
    print(datetime(year,month,30).strftime("%A"))
except ValueError as ex:
    # in February there is no 30 days
    print(f"No such date - {year}-{month}-30")

Keep in mind that you have to think about another logic for February...

Upvotes: 1

Related Questions