Number of days in particular month of particular year?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Getting the number of days in a given month looks easy until leap years enter the picture. This logic appears in billing, booking, reporting, and validation code, and small mistakes around February or century years can produce surprisingly expensive bugs.
The safest approach is to rely on a standard library when possible. If you need to implement the logic yourself, make sure you are using the Gregorian leap-year rules correctly.
The Leap-Year Rule
For the Gregorian calendar, February changes depending on whether the year is a leap year.
A year is a leap year if:
- it is divisible by
4 - except years divisible by
100 - except those divisible by
400, which are leap years again
So:
- '
2024is a leap year' - '
1900is not a leap year' - '
2000is a leap year'
That is the part people most often oversimplify.
Manual Implementation in Python
A clear hand-written implementation is useful for understanding and for small standalone logic.
This function is explicit, testable, and easy to review.
Use Standard Libraries in Real Code
In production code, standard library date APIs are usually better because they avoid duplicated calendar logic.
Python example:
Java example:
Swift example:
These library-based solutions reduce the chance of edge-case mistakes and usually communicate intent better.
Input Validation Matters
The calendar rule is only part of the problem. Real bugs also come from bad inputs:
- month
0 - month
13 - text that was parsed loosely into an invalid integer
- timezone conversions mixed into what should be a pure month-length lookup
If the values come from timestamps, convert to the correct local date first and then ask for the month length. Otherwise you can accidentally shift into a different month at a timezone boundary.
Testing the Edge Cases
At minimum, test the leap-year edges and invalid month values.
These few cases catch most logical regressions immediately.
Common Pitfalls
A common mistake is treating “divisible by 4” as the whole leap-year rule and ignoring the century exceptions.
Another issue is returning a default value such as 0 for invalid months instead of failing clearly. Silent fallback values make bad input harder to diagnose.
Developers also sometimes reimplement this logic in every service even though their language already provides a standard library answer.
Finally, avoid mixing timezone conversion, timestamp parsing, and month-length calculation in one opaque function. Keep the calendar logic simple and focused.
Summary
- Month length depends on the Gregorian leap-year rule, especially for February.
- The correct leap-year rule includes the
100and400year exceptions. - Standard library APIs are usually the safest production solution.
- Validate month input before calculating the result.
- Add a few leap-year edge cases to tests so this logic stays correct.

