Compare Two Dates

Take two dates (day and month) and determine which one comes first.

AdvancedPhase 1 - Level 5: Creative / Tricky Logical ScenariosExample 8 of 10
compare-two-dates.py
1# Take first date
2day1 = int(input("Enter first day: "))
3month1 = int(input("Enter first month: "))
4
5# Take second date
6day2 = int(input("Enter second day: "))
7month2 = int(input("Enter second month: "))
8
9# Compare dates
10if month1 < month2:
11 print("First date comes earlier")
12elif month1 > month2:
13 print("Second date comes earlier")
14else: # Same month
15 if day1 < day2:
16 print("First date comes earlier")
17 elif day1 > day2:
18 print("Second date comes earlier")
19 else:
20 print("Same date")

Output

Enter first day: 15
Enter first month: 3
Enter second day: 20
Enter second month: 3
First date comes earlier

Enter first day: 15
Enter first month: 3
Enter second day: 10
Enter second month: 4
First date comes earlier

What's going on

Compare months first, then days if months are same.

Key Concepts:

Compare months first
If months equal, compare days
Handle all cases: earlier, later, same