Answer:
The solution code is written in Python 3
- def is_older(date1, date2):
- date1_list = date1.split("/")
- date2_list = date2.split("/")
-
- for i in range(0, len(date1_list)):
- date1_list[i] = int(date1_list[i])
- date2_list[i] = int(date2_list[i])
-
- if(date1_list[2] > date2_list[2]):
- return False
- elif(date1_list[2] < date2_list[2]):
- return True
- else:
- if(date1_list[1] > date2_list[1]):
- return False
- elif(date1_list[1] < date2_list[1]):
- return True
- else:
- if(date1_list[0] >= date2_list[0]):
- return False
- else:
- return True
-
- print(is_older("2/14/2020", "2/13/2019"))
- print(is_older("1/15/2019","1/15/2020"))
- print(is_older("3/10/2020", "3/10/2020"))
Step-by-step explanation:
Let's presume the acceptable date format is (mm/dd/yyyy). We create a function is_older that takes two input dates (Line 1). Next we use split method to convert the two date strings into list of date components, month, day and year (Line 2-3).
The codes in Line 5-7 are to convert each number in the list from string to numerical type.
Next, we do the date comparison started with the year which is indexed by 2. If the year in date1_list is greater than the date2_list, return False (Line 10). If it is smaller, return True (Line 12). If it is equal, then we need to go on to compare the month component (Line 14 - 17). The comparison logic is similar to checking the year.
If the month of the two dates are the same, we go on to compare the day (Line 19 -22). If the day in date1_list is greater or equal to the date2_list, it shall return False, else it return True.
We test the function using three sample inputs (Line 24-25) and the output is as follows:
False
True
False