Sub topic 12
DATEDIF units and why MD is unreliable
This chapter assumes you can enter formulas, use cell references such as A2, and know that dates are stored as serial numbers. The chapter before it introduced DATEDIF and the Y unit. Here you will cover all six units and see why one of them should be avoided.
The six units
DATEDIF(start, end, unit) takes the unit as quoted text. The start date must not be later than the end date, or the result is #NUM!. An unrecognized unit also gives #NUM!.
| Unit | Returns |
|---|---|
| "Y" | Complete years between the dates |
| "M" | Complete months between the dates |
| "D" | Days between the dates |
| "YM" | Complete months left over after removing whole years |
| "YD" | Days left over after removing whole years |
| "MD" | Days left over after removing whole months and years |
The first three count the full span. The last three count only the remainder, so they are meant to be combined with other units.
A worked example
Put 15 February 2020 in A2 and 10 May 2024 in B2. Each formula below counts only complete periods.
=DATEDIF(A2,B2,"Y")This returns 4. The fourth anniversary was 15 February 2024, and the fifth has not arrived.
=DATEDIF(A2,B2,"M")This returns 50. From February 2020 to May 2024 is 51 calendar months, but the 15th of May has not been reached, so only 50 are complete.
=DATEDIF(A2,B2,"YM")This returns 2. It is the 50 complete months minus 48 months in the 4 whole years.
=DATEDIF(A2,B2,"YD")This returns 85. It counts the days from the last anniversary (15 February 2024) to 10 May 2024. That period includes 29 days of February 2024.
Why MD is unreliable
The MD unit is meant to give the days left after the last complete month. Microsoft's documentation warns that it can return a negative number, zero, or an inaccurate result. The problem shows up when the start day is later in the month than the end day, or when the start day does not exist in a nearby shorter month, such as the 31st. Excel does not flag the bad result with an error. The cell just shows a plausible number, so the mistake is easy to miss.
A safer replacement for MD
Compute the date of the last completed month yourself with EDATE, then subtract it from the end date. This uses only serial-number arithmetic, which you can check by hand.
=B2-EDATE(A2,DATEDIF(A2,B2,"M"))For the example dates, DATEDIF returns 50 months and EDATE(A2,50) is 15 April 2024. Subtracting that from 10 May 2024 gives 25 days. For a start date of 31 January, EDATE clamps to the month end, so the result is still a valid, non-negative day count.
You can then assemble a full description from reliable parts.
=DATEDIF(A2,B2,"Y")&" y, "&DATEDIF(A2,B2,"YM")&" m, "&(B2-EDATE(A2,DATEDIF(A2,B2,"M")))&" d"For the example dates, this returns "4 y, 2 m, 25 d".
Exercise
A2 holds 20 March 2023 and B2 holds 5 July 2024. Which formula gives the days left after the last complete month without using the "MD" unit, and what does it return?
Show answer
Use =B2-EDATE(A2,DATEDIF(A2,B2,"M")). DATEDIF with "M" returns 15 complete months (March 2023 to June 2024 is 15, and 20 July has not been reached). EDATE(A2,15) is 20 June 2024. Subtracting that from 5 July 2024 gives 15 days.