Python Program Find Day of Week For Given Date

Python program to find day of the week for a given date; Through this tutorial, i am going to show you how to find and display day of the week for a given date in python.

Python Program Find Day of Week For Given Date

  • Algorithm to find day of the week or weekday for a given date
  • Write a python program to find the day of the week for a given date and print weekday

Algorithm to find day of the week or weekday for a given date

  1. Import calendar module in the program.
  2. Take a date from the user in the form of date(d) – month(m) -year(y).
  3. Check the given date is valid or not.
    1. If the date is valid then execute the next statement in program.
    2. If date is invalid then show ‘you have entered an invalid date’ to the user.
  4. At the end of the program, Print the day of the week/weekday of the given date.

Write a python program to find the day of the week for a given date and print weekday

#Find the day of the week for a given date in Python
# import calendar module
import calendar
d,m,y=map(int,input('Enter the value of date,month and year: ').split())
a=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
try:
    s=calendar.weekday(y,m,d)
    print('Weekday:',a[s])
except ValueError:
    print('You have entered an invalid date.')

Output

Enter the value of date,month and year:  05 04 2020
Weekday: Sunday

Recommended Python Tutorials

Leave a Comment