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

The Excel formula =DATEDIF(start_date, end_date, "MD") is designed to calculate the number of remaining days between two dates after accounting for complete years and complete months. However, Microsoft has officially documented that using the "MD" parameter frequently results in an inaccurate calculation, a negative number, or a #NUM! error.

This behavior is not user error. It is a long-standing arithmetic bug inside the legacy DATEDIF engine that occurs when the day value of the end date is smaller than the day value of the start date across months with differing day counts (such as January to February or March to April).

Why Does DATEDIF “MD” Return Negative Numbers?

The DATEDIF “MD” argument contains an internal calculation bug that ignores the differing day counts of preceding months. When the end date’s day-of-the-month is lower than the start date’s day-of-the-month, the function subtracts the start day from an incorrect prior month’s length, resulting in negative days (e.g., -2 or -3). Microsoft advises against using “MD”; use modern date arithmetic instead.


Anatomy of the “MD” Calculation Failure

To understand why the bug occurs, look at how DATEDIF attempts to calculate remaining days:

Suppose you evaluate:

=DATEDIF("2024-01-31", "2024-03-01", "MD")
  1. The start date is January 31.
  2. The end date is March 1.
  3. Logically, between January 31 and March 1 in a leap year (2024), February has 29 days. From Jan 31 to Feb 29 is 29 days, plus 1 day to reach March 1, which equals 1 full month and 0 remaining days.
  4. Instead of referencing February’s 29 days, the internal algorithm uses the calendar length of the prior reference month or assumes standard 30/31-day intervals.
  5. It performs an internal subtraction: 1 (March day) - 31 (January day) = -30, then adds a static day count, frequently outputting 1, 2, or wildly inaccurate positive numbers like 28.

Microsoft’s official documentation includes the following warning:

“The ‘MD’ argument may result in a negative number, a zero, or an inaccurate result. We recommend using an alternative formula.”


Reliable Formula Alternatives to Replace DATEDIF “MD”

Because Microsoft retains DATEDIF solely for backward compatibility with Lotus 1-2-3 without patching internal mechanics, you should replace any "MD" implementation with robust native Excel formulas.

To find the exact remaining days between two dates after subtracting complete months, roll the end date back to the same day of the prior month, then subtract that date from the actual end date.

Assuming A2 contains the Start Date and B2 contains the End Date:

=B2 - EDATE(B2, IF(DAY(B2) < DAY(A2), -1, 0)) + IF(DAY(B2) < DAY(A2), 0, -DAY(A2))

A simpler and cleaner method uses the EDATE function combined with DATEDIF‘s stable "M" parameter:

=B2 - EDATE(A2, DATEDIF(A2, B2, "m"))

How this formula works:

  1. DATEDIF(A2, B2, "m") accurately calculates the total number of complete calendar months between the two dates (the "m" parameter is not affected by the bug).
  2. EDATE(A2, ...) advances the start date forward by that exact number of complete months.
  3. B2 - ... subtracts that advanced date from the end date using standard serial day subtraction.
  4. Because standard subtraction operates on Excel’s true date serial numbers, the result is guaranteed to be positive and accounts for the exact number of days in every leap year and month.

Example Walkthrough:

Start Date (A2)End Date (B2)=DATEDIF(A2, B2, "MD")Alternative Formula Result
2023-01-312023-03-01-2 (Bugged)1 day (Accurate)
2024-01-312024-03-01-1 (Bugged)0 days (Accurate leap year)
2023-05-152023-06-102626 days (Accurate)
2023-08-312023-10-01-2 (Bugged)1 day (Accurate)

Building a Bulletproof “X Years, Y Months, Z Days” String

A frequent requirement in HR, payroll, and tenure tracking is generating employee service strings like "5 Years, 3 Months, 12 Days".

When built with "MD", these strings eventually break, displaying output such as "2 Years, 1 Months, -3 Days".

The Complete Replacement Formula

Use this formula to generate the full tenure string safely:

=DATEDIF(A2, B2, "y") & " Years, " &
 DATEDIF(A2, B2, "ym") & " Months, " &
 (B2 - EDATE(A2, DATEDIF(A2, B2, "m"))) & " Days"
  • DATEDIF(A2, B2, "y"): Accurately returns full completed years.
  • DATEDIF(A2, B2, "ym"): Accurately returns remaining months ignoring completed years (the "ym" switch does not suffer from the negative calculation bug).
  • B2 - EDATE(A2, DATEDIF(A2, B2, "m")): Accurately computes remaining days without using "MD".

Edge Cases and Adjacent Date Formula Errors

When implementing date difference calculations across large datasets, watch for these related traps:

Inverted Dates Triggering #NUM!

DATEDIF requires that the start_date be less than or equal to the end_date. If data entry errors cause your timeline to flow backward, the function does not return a negative number; it halts with a #NUM! error. If you encounter this across your sheets, see #NUM! in DATEDIF: When Start Date is after End Date.

Leap Year Tenure Drift

When employees start on February 29 of a leap year, standard anniversary calculations require specific policy rules (whether their work anniversary falls on February 28 or March 1 in non-leap years). To align organizational policy with Excel’s day-count rules, refer to Tenure Calculation: Handling leap years in employee “Years of Service” formulas.

End-of-Month Boundary Discrepancies

When calculating tenure between August 31 and September 30, both are the final day of their respective months. DATEDIF evaluates calendar day values strictly (30 < 31), meaning it does not treat September 30 as a full completed month after August 31. If your models track vacancy intervals, property turnover, or ticket queues across different calendar months, consult Unit Turnaround Time: Fixing DATEDIF errors when a unit is vacant across calendar years. and Backlog Aging: Using DATEDIF to categorize tasks by “Weeks in Queue.”.

Finally, if your calculation requires excluding weekends and company holidays rather than measuring elapsed calendar days, transition from DATEDIF to Workday Calculation: Fixing NETWORKDAYS when it fails to account for regional bank holidays. to establish correct business day counts.