Find the day of the week for a given date in the past or future in Python

Here, we will figure out how to discover the day of the week for a given specific date before or future in the Python programming language?

In this issue, a specific date will be given by the client which might be from the past or future and we need to discover the weekday.

To this, we will utilize the schedule module which gives us different capacities to tackle the issue identified with date, month and year. Prior to going to discover

the weekday of a given specific date, we need to check whether the given date is substantial or not. In the event that the given date isn’t legitimate, at that point, we will get some blunder.

Thus, to defeat this kind of blunder we will utilize the attempt aside from articulation.

Syntax of attempt with the exception of the proclamation:

    try:
        #statement 
    except error_types:
        #statement

Algorithm to tackle this issue:

  • Import schedule module in the program.
  • Take a date from the client as date(d) – month(m) – year(y).
  • Check the given date is substantial or not.
  • On the off chance that the date is legitimate, at that point execute the following articulation.
  • On the off chance that the date is invalid, at that point show ‘you have entered an invalid date’ to the client.
  • Print the weekday of the given date.

How about we start composing the Python program by the execution of the above algorithm.

Code:

# importing the 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:

RUN 1:
Enter the value of date, month and year: 28 10 2019
Weekday: Monday

RUN 2:
Enter the value of date, month and year: 32 10 2019
You have entered an invalid date.

Leave a Comment

error: Alert: Content is protected!!