DATEDIF “MD” Bug: Why calculating “Days between months” can result in negative numbers

The DATEDIF function’s “MD” (Month-Day) argument is a documented legacy bug in the Excel calculation engine. While intended to return the difference between days in two dates, ignoring months and years, it frequently returns negative numbers or gross inaccuracies when the end date follows a month with fewer days than the start date’s day value (e.g., comparing the 31st of one month to the 1st of the next).

Fast-Fix: The 45-Second Solution

The DATEDIF “MD” bug occurs because Excel’s internal logic fails to correctly “wrap” day counts when transitioning between months of varying lengths (especially February or 30-day months). Because this is a known compatibility carry-over from Lotus 1-2-3, Microsoft does not provide a patch. First Aid: Immediately replace "MD" arguments with a manual subtraction logic: =IF(DAY(End)>DAY(Start), DAY(End)-DAY(Start), DAY(End)-DAY(Start)+DAY(EOMONTH(End,-1))).

Quick Risk Snapshot

  • Severity Tier: Moderate (Logical Error)
  • Is it safe to ignore? No. Negative day counts in durations break downstream financial models and aging reports.
  • Most common cause: Comparing a start date with a high day value (e.g., 31st) to an end date in a shorter subsequent month.
  • Rare/Serious cause: Cumulative errors in leap year calculations within multi-year project schedules.

Low Risk vs. High Risk

  • If it’s a small range (within the same 31-day month): Low Risk. The bug usually remains dormant if the day-of-month values do not cross a “short month” boundary.
  • If it’s a Finance/HR model (Payroll, Accruals, or Tenure): High Risk. A negative “days” value in a tenure string (e.g., “5 Years, 2 Months, -3 Days”) causes #VALUE! errors in string concatenations and undermines audit trails.

The Mechanics of the Break

The DATEDIF engine is not a native Excel construct; it is a legacy function maintained for backward compatibility. When using the “MD” argument, Excel attempts to subtract the day of the start date from the day of the end date after “normalizing” the months.

The break happens during the normalization phase. If the previous month had 28 or 30 days and your start day was 31, the internal pointer essentially “falls off” the calendar grid, resulting in a negative integer. The engine lacks the logic to check the EOMONTH (End of Month) length of the preceding period relative to the start day’s ordinal value.

Probability Breakdown

  • Likely (85%): Comparing a date on the 29th, 30th, or 31st to a date in the following month.
  • Possible (10%): Using DATEDIF on dates prior to 1900 (not supported).
  • Rare (5%): System clock or regional setting mismatches affecting serial date interpretation.

What Escalates the Risk

The risk compounds when DATEDIF results are used in Dynamic Arrays or VLOOKUP tables. If a formula expects a positive integer and receives -2, it may trigger a range error or, worse, pull data from the wrong index in a sorted table without throwing a visible error flag.

Consequence Timeline

  • 24 Hours: Individual cell displays a negative number or #NUM!; visually obvious but annoying.
  • 1 Week: Aggregated “Average Project Duration” metrics become skewed; reporting integrity is compromised.
  • 1 Month: Forensic auditors flag the model due to inconsistent logic; manual recalculation of all date-based intervals is required.

Common Confusion Fix

Do not confuse the “MD” bug with a standard #VALUE! error.

  • #VALUE!: Usually means your “dates” are actually text strings.
  • “MD” Bug: The formula looks correct and the inputs are valid dates, but the output is mathematically impossible (e.g., -5 days).

What To Do Right Now

  1. Stop using "MD" for any production-level reporting.
  2. Audit your workbook for the string DATEDIF( using Ctrl+F.
  3. Replace the “MD” segment with a more robust logic string. For example, if your start date is in A1 and end date is in B1:
    =B1-DATE(YEAR(B1),MONTH(B1)-(DAY(B1)<DAY(A1)),DAY(A1))

Hard-Stop Triggers

  • If the DATEDIF result is used to calculate Interest Accrual or Penalty Fees.
  • If your workbook contains thousands of rows where manual verification is impossible.
  • If the workbook is used for Regulatory Compliance (e.g., FDA shelf-life or Basel III reporting).

Professional Audit Path

An Excel Consultant will perform the following:

  1. Trace Precedents: Identify if the start/end dates are hard-coded or volatile.
  2. Stress Test: Input “Feb 28” vs “March 31” to see if the model breaks.
  3. Standardize: Convert all date calculations to the YEARFRAC function or custom DATE subtraction for 100% precision.

Complexity/Repair Range

  • Minor (Logical Fix): Replacing a few dozen formulas with the corrected subtraction logic.
  • Moderate (Architecture): If the negative number has already been “hard-coded” via Paste-Values into a historical database.

Symptom Escalators

Diagnostic Summary

The “MD” argument is functionally obsolete for professional-grade modeling. Because Microsoft has explicitly stated they will not fix this legacy bug, you must pivot to manual date-math logic. Treat any existing “MD” calculations as high-risk liabilities until they are replaced with stable DATE and DAY function combinations.