# Python Program to check if a date is valid or not

February 22, 2022, Learn eTutorial
1425

## How to check date is valid in python?

In this python program, we need to check if a given date is valid or invalid. for that, we have to check the days and months are correct or not.

For the month validation, check the month is in between the number 1 and 12. if it is greater than 12 or less than 1, the date is not valid.

To check whether the day is valid or not by taking the following conditions

• if the month is 1, 3, 5, 7, 8, 10, or 12 then the maximum value for the day is 31; else it is invalid.
• If the month is 4, 6, 9, or 11 then the day's max value is 30. if the day is more than 30 the date is invalid.
• Finally, to confirm the days of February we check whether the year is a leap year or not by using a mod operator with the year. If the remainder after the mod operation with 4 is equal to zero, and year mod 100 is not equal to zero [please refer to leap year python program for more details], it's a leap year, then the maximum days will be 29 else will be 28. If the entered date is not validating the leap year, print 'not valid'.

## How to apply this date validation logic in python program?

In python, we are using a `split date` function to split the month, day, and year separately, and then we convert them into an integer using `int()` data type. Now, we use an `if condition` and `elif condition`  to check all the conditions we discussed above.

### ALGORITHM

STEP 1: Accept the input from the user using the input method into a date.

STEP 2: Split the date using the separator ' / ' into dd, mm, and yy.

STEP 3: Assign the values of dd, mm, and yy into the respective variable using `int()` data type.

STEP 4: Using an `if condition` we check the month is 1, 3, 5, 7, 8, 10, or 12, and if so then assign the max value for the day as 31.

STEP 5: Using an `elif` we check the month is 4, 6, 9, or 11, then assign the max value to 30.

STEP 6: Using an `if` condition check for the year is a leap year or not by taking the mod as yy % 4 == 0 and yy % 100 != 0 or yy % 400 == 0. then it's a leap year so assign the max as 29.

STEP 7: Using an `else`, it's not a leap year so max is 28.

STEP 8: Check the month validation by using `if condition` that check month is less than 1 or greater than 12 then print the date is invalid.

STEP 9: Using `elif` check the day is less than 1 or greater than our max value the print the date is not valid.

STEP 10: Use the else statement to `print` the entered date is valid.

For the date validation program, we are using the below python concepts, please refer to those for a better understanding

## Python Source Code

```                                          ```date=input("Enter the date: ")

dd,mm,yy=date.split('/')     # splitting the date to day month and year

dd=int(dd)

mm=int(mm)

yy=int(yy)

if(mm==1 or mm==3 or mm==5 or mm==7 or mm==8 or mm==10 or mm==12):

max=31

elif(mm==4 or mm==6 or mm==9 or mm==11):    # checking for the day is valid or not,  finding max value

max=30

elif(yy % 4 == 0 and yy % 100 != 0 or yy % 400 == 0):   # leap year check

max=29
else:

max=28

if(mm < 1 or mm > 12):

print("Date is invalid.")

elif(dd < 1 or dd > max):

print("Date is invalid.")

else:

print("Date you entered is valid")```
```

## OUTPUT

```Enter the date : 5/7/2016

Date you entered is valid

```

VIEW ALL
VIEW ALL