I have already defined a function to check if a given date is a valid date or not. Now I must find out a) difference between 2 dates b) date 2 should not be less than date 1 c) date 2 and date 1 must be valid dates. I have to incorporate the first function (i.e., valid date function) into the second function.
import datetime
def is_valid_date (year, month, day):
"""
check if the input date is a valid date or not.
"""
if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
max_days = 31
elif month == 4 or month == 6 or month == 9 or month == 11:
max_days = 30
elif year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
max_days = 29
else:
max_days = 28
if (month < 1 or month > 12) or (day < 1 or day > max_days) or (year < 1 or year > 9999):
return "Invalid Date"
else:
return "Valid Date"
def days_between(year, month, day, year1, month1, day1):
result = is_valid_date (year, month, day)
if datetime.date (year1, month1, day1) != result:
return 0
elif datetime.date (year,month,day) != result:
return 0
elif datetime.date (year1, month1, day1) < datetime.date (year, month, day):
return 0
else:
date1= (year, month, day)
date2= (year1, month1, day1)
difference = datetime.date (date2) - datetime.date (date1)
return difference
print (is_valid_date(2000, 12,1))
print (days_between(2000,12,1,2000,12,20))
Code is returning the following answers:
Valid Date
0
What I would like to get:
Valid Date
19