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.
1# Take first date2day1 = int(input("Enter first day: "))3month1 = int(input("Enter first month: "))45# Take second date6day2 = int(input("Enter second day: "))7month2 = int(input("Enter second month: "))89# Compare dates10if month1 < month2:11 print("First date comes earlier")12elif month1 > month2:13 print("Second date comes earlier")14else: # Same month15 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
Read both day-month pairs.
- 2
Compare months first.
- 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.
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum