Sub topic 12
DATEDIF units and why MD is unreliable
This chapter assumes you can enter formulas and use cell references. It also assumes you know that Excel stores a date as a serial number and that you have seen DATEDIF return complete years or months. Here you will learn exactly what each of the six units counts, how they combine, and why one of them, MD, should not be trusted.
The six units at a glance
The syntax is =DATEDIF(start_date, end_date, unit). The unit is a text value in quotation marks. It is not case-sensitive. Excel does not list DATEDIF in the function autocomplete, but it still calculates it.
| Unit | What it counts | Typical use |
|---|---|---|
| `"Y"` | Complete years between the dates | Age in years |
| `"M"` | Complete months between the dates | Total tenure in months |
| `"D"` | Days between the dates | Same as `end - start` |
| `"YM"` | Complete months left over after removing complete years | The months part of "3 years, 2 months" |
| `"YD"` | Days left over after removing complete years | Days since the last anniversary |
| `"MD"` | Days left over after removing complete months | The days part of "2 months, 9 days" |
The first three units are totals. The last three are remainders, and each one ignores the larger units. That is why they only make sense when you pair them with the unit they leave out.
Totals versus remainders
Take a start date of 15 January 2024 and an end date of 10 March 2024.
=DATEDIF(DATE(2024,1,15),DATE(2024,3,10),"M")This returns 1. Counting from 15 January, the first complete month ends on 15 February. The second would end on 15 March, which is after the end date.
=DATEDIF(DATE(2024,1,15),DATE(2024,3,10),"D")This returns 55: 31 days from 15 January to 15 February, then 24 more days to 10 March.
=DATEDIF(DATE(2024,1,15),DATE(2024,3,10),"MD")This returns 24, the days after the one complete month. Here MD agrees with the true answer, so it looks safe. The next sections show why it is not safe in general.
Years and the YM and YD remainders
Use a longer span: 15 March 2022 to 10 March 2023, which is one day short of a year.
| Formula unit | Result | Why |
|---|---|---|
| `"Y"` | 0 | The anniversary on 15 March 2023 has not arrived |
| `"M"` | 11 | 15 March to 15 February is 11 complete months |
| `"YM"` | 11 | No complete years, so all 11 months remain |
| `"YD"` | 360 | 365 days in the full year, minus the 5 days from 10 to 15 March |
| `"MD"` | 23 | 15 February to 10 March 2023 |
"YM" equals "M" only while fewer than 12 months have passed. After that, "M" keeps growing and "YM" wraps back to 0 at each anniversary. Compare "M" and "YM" on a three-year span to see this.
Two edge cases matter for "Y" and "YD":
- Someone who starts on 29 February 2020 has no true anniversary in 2023.
DATEDIFreturns 2 for"Y"on 28 February 2023 and 3 on 1 March 2023. "YD"ignores the year but not leap days. A span that crosses 29 February can be off by one compared with what you expect from counting on a calendar. If your result depends on that day, check it against a manual count.
How the units combine
A readable duration joins a total unit with the remainder units below it. The valid pairings are:
"Y"with"YM", and optionally"MD", for years, months, days."M"with"MD"for months and days when the total months matter, such as a 26-month contract."Y"with"YD"for years and days since the last anniversary.
Why MD is unreliable
Microsoft's own documentation warns that "MD" has known problems. It says the result may be a negative number, a zero, or an inaccurate value. The cause is how the unit is calculated. It does not count forward from the last complete month to the end date. Instead it works from the calendar month before the end date, and short months and end-of-month start dates throw it off. The exact wrong answers also differ between Excel versions, and other spreadsheet programs implement the unit differently.
Start dates on the 29th, 30th or 31st are the usual trigger, because many months lack those days. A run of results that looks correct for ordinary dates can break only on those. That makes the bug hard to catch in testing.
=DATEDIF(DATE(2024,1,31),DATE(2024,3,1),"MD")Do not rely on the number this returns. The correct answer depends on a definition: Excel's own EDATE moves 31 January forward one month to 29 February, which leaves 1 day to 1 March. A formula you can explain from first principles is better than one whose answer you have to look up.
A reliable replacement for MD
Count complete months with "M", move the start date forward by exactly that many months with EDATE, and subtract. This uses the clamping rule from the EDATE chapter, so the result follows a rule you already know.
=B2-EDATE(A2,DATEDIF(A2,B2,"M"))With A2 holding 15 January 2024 and B2 holding 10 March 2024:
DATEDIF(A2,B2,"M")returns 1.EDATE(A2,1)returns 15 February 2024.B2 - 15 February 2024returns 24.
This matches the "MD" result for ordinary dates and stays non-negative for any valid pair. EDATE never returns a date past the end date when the month count comes from DATEDIF, so the subtraction cannot go below zero.
A full years, months, and days text built the safe way looks like this, with the start in A2 and the end in B2:
=DATEDIF(A2,B2,"Y")&" years, "&DATEDIF(A2,B2,"YM")&" months, "&(B2-EDATE(A2,DATEDIF(A2,B2,"M")))&" days"For 15 March 2022 to 10 March 2023 it returns 0 years, 11 months, 23 days.
Errors and limits
DATEDIF fails quietly in some cases and loudly in others.
- If the start date is later than the end date, it returns
#NUM!. It does not swap them for you. - An unrecognised unit, such as
"MM"or a unit without quotation marks, returns#NUM!or a name error. - Text that looks like a date but is not a real serial number returns
#VALUE!. Check with=ISNUMBER(A2). - A date stored as 0 or an empty cell is treated as the serial number 0, which produces large and misleading spans.
To guard against reversed dates, wrap the call:
=IF(A2>B2,"Start is after end",DATEDIF(A2,B2,"Y"))Choosing between DATEDIF and other approaches
Use DATEDIF when you want complete years or complete months, as in age, tenure, or contract length. It saves you from building the same logic with YEAR, MONTH, and DAY. Avoid it when you need fractional years, where YEARFRAC is the better tool. Avoid "MD" entirely and use the EDATE subtraction above. Because DATEDIF is undocumented in the function list, test each formula on a few boundary dates, such as an end date one day before and on an anniversary, before you depend on it.
Exercise
A2 holds 31 March 2023 and B2 holds 10 June 2023. Write a formula that returns the number of days left after counting complete months, without using the "MD" unit. What does it return?
Show answer
Use =B2-EDATE(A2,DATEDIF(A2,B2,"M")). DATEDIF with "M" returns 2, because 31 March to 30 May is two complete months and 30 June is after the end date. EDATE(A2,2) returns 31 May 2023. Subtracting gives 10 June minus 31 May, which is 10 days.
Exercise
Which pair of units gives the number of whole years and the months left over, and why can't you use "M" alone for the months part of "3 years, 2 months"?
Show answer
Use "Y" and "YM". "M" is a total, so for a span of 3 years and 2 months it returns 38, not 2. "YM" removes the complete years first and returns 2.