Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Compare Two Dates

Compare two day-and-month dates in Python without a date library. Students prioritize the month, break ties with the day, and recognize missing year validation.

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

In this one

  • Compare fields by priority
  • Break month ties with days
  • Recognize missing year validation

How it works

  1. 1

    Read both day-month pairs.

  2. 2

    Compare months first.

  3. 3

    If they tie, compare days or print Same date.

Edge cases

Is March 31 earlier than April 1?

Is March 31 earlier than April 1?

Yes. The month comparison decides before the day comparison.

Are identical dates detected?

Are identical dates detected?

Yes. Both month and day comparisons tie.

What's going on

Calendar order within one year depends on the month first. A smaller month is earlier even if its day number is larger than the other date's day.

Only when months match does the nested chain compare days. Equal months and days produce Same date. The program assumes valid dates in a common year; it neither reads a year nor checks whether April has a thirty-first day.

Is March 31 earlier than April 1? Yes. The month comparison decides before the day comparison.

Are identical dates detected? Yes. Both month and day comparisons tie.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is compared first?

Pick an answer to check it.

Question 2

Is March 31 earlier than April 1?

Pick an answer to check it.

Questions people ask

Is March 31 earlier than April 1?

Yes. The month comparison decides before the day comparison.

Are identical dates detected?

Yes. Both month and day comparisons tie.

What is compared first?

Month.