Classify Natural-Month Distance Between Dates
Learn this problemProblem statement
You are given two valid Gregorian calendar dates, firstDate and secondDate, in YYYY-MM-DD format. Ignore their input order: let earlier be the smaller date and later the larger date.
Compute the one-month anniversary of earlier by moving to the next calendar month and preserving its day when possible. If that day does not exist in the next month, clamp it to that month's final valid day. Compare later with the anniversary and return exactly one of:
LESS_THAN_ONE_MONTHEXACTLY_ONE_MONTHMORE_THAN_ONE_MONTH
This is a calendar comparison, not a fixed elapsed-day calculation. Time zones do not apply.
Function
classifyNaturalMonthDistance(firstDate: String, secondDate: String) → StringExamples
Example 1
firstDate = "2026-01-15"secondDate = "2026-02-14"return = "LESS_THAN_ONE_MONTH"The January 15 anniversary is February 15, and February 14 is earlier.
Example 2
firstDate = "2026-01-31"secondDate = "2026-02-28"return = "EXACTLY_ONE_MONTH"February has no day 31 in 2026, so the anniversary clamps to February 28.
Example 3
firstDate = "2026-03-30"secondDate = "2026-02-28"return = "MORE_THAN_ONE_MONTH"The inputs are ordered first. One month after February 28 is March 28, which is before March 30.
Constraints
- Both inputs are valid Gregorian dates in zero-padded
YYYY-MM-DDformat. 1900-01-01 <= firstDate, secondDate <= 2100-12-31.- The two dates may be supplied in either order and may be equal.
- Use the standard Gregorian month lengths, including February's leap-year length.
- The monthly anniversary clamps to the final valid day only when the original day is absent from the next month.