Sub topic 11
DATEDIF: counting complete years, months, and days
This chapter assumes you can enter formulas, use cell references such as A2, and apply a number format. It also assumes you know that Excel stores a date as a serial number, and that subtracting two dates returns a day count. DATEDIF builds on that: it counts how many complete calendar periods fit between two dates.
What DATEDIF does
Subtracting dates gives days. Years and months have uneven lengths, so a day count does not convert cleanly into them. DATEDIF handles that by counting whole periods only.
=DATEDIF(start_date, end_date, unit)start_dateis the earlier date.end_dateis the later date.unitis a text code in quotes that says what to count. It is not case sensitive.
DATEDIF is a legacy function kept for compatibility with Lotus 1-2-3. Excel does not list it in the formula autocomplete or the Insert Function dialog, so you must type it in full. It still calculates normally in current versions.
Complete periods, not rounded ones
A period counts only once it has fully elapsed. Someone born on 15 January 2000 is 23 years old on 14 January 2024 and 24 on 15 January 2024. DATEDIF returns 23 on the first date and 24 on the second. Nothing is rounded.
For the units used in this chapter, Excel works out the answer by comparing the calendar parts of the two dates:
- Take the difference in years or months from the year and month numbers.
- If the end date's day of the month is earlier than the start date's day, subtract one, because the last period is not yet complete.
This comparison of day numbers explains the edge cases later in the chapter.
A worked example
Take a start of 15 January 2020 and an end of 10 March 2023. All of the following formulas are self-contained.
=DATEDIF(DATE(2020,1,15),DATE(2023,3,10),"Y")
=DATEDIF(DATE(2020,1,15),DATE(2023,3,10),"M")
=DATEDIF(DATE(2020,1,15),DATE(2023,3,10),"D")
=DATEDIF(DATE(2020,1,15),DATE(2023,3,10),"YM")
=DATEDIF(DATE(2020,1,15),DATE(2023,3,10),"YD")| Unit | Result | Meaning |
|---|---|---|
| `"Y"` | 3 | Complete years. 15 Jan 2023 is passed, 15 Jan 2024 is not. |
| `"M"` | 37 | Complete months in total. 15 Feb 2023 is passed, 15 Mar 2023 is not. |
| `"D"` | 1150 | Days between the dates. This equals subtracting the serial numbers. |
| `"YM"` | 1 | Complete months left over after removing the complete years (37 − 36). |
| `"YD"` | 54 | Days from 15 Jan 2023 to 10 Mar 2023, ignoring the years. |
"Y", "M", and "D" are totals in one unit. "YM" and "YD" are remainders: they ignore the larger unit. That makes them suitable for the second half of a sentence such as "3 years, 1 month".
The sixth unit, "MD", returns the leftover days after removing complete months. It looks like the natural third part of an age, but it can return wrong results. The next chapter covers it.
How the units fit together
Building an age or tenure
The most common use is to show an elapsed time as text. With a start date in A2:
=DATEDIF(A2,TODAY(),"Y")&" years, "&DATEDIF(A2,TODAY(),"YM")&" months"Each half counts something different. "Y" counts the complete years. "YM" counts the months after those years, so it is always between 0 and 11. The & operator joins the two numbers and the labels into one text string.
Because the formula uses TODAY(), it recalculates whenever the workbook does. The age updates itself, but a saved report will also change when someone reopens it on a later date.
If you need the total number of months, use "M" alone. This is common for contract lengths and subscription billing:
=DATEDIF(A2,B2,"M")Guarding against errors
DATEDIF returns #NUM! in two situations:
- The start date is later than the end date.
- The unit code is not one of the six valid ones, for example
"Months"or a missing quote.
It returns #VALUE! if either date is text that Excel cannot read as a date. A later chapter covers text dates in detail.
The start-after-end case often appears with TODAY(), for example when A2 holds a future start date. Test for it first:
=IF(A2>TODAY(),"Not started",DATEDIF(A2,TODAY(),"Y")&" years, "&DATEDIF(A2,TODAY(),"YM")&" months")Blank cells are a related trap. An empty A2 is read as 0, which Excel treats as a date in 1900 (or earlier, depending on the date system), so the age shows more than a century. Test with IF(A2="","",...) when the data may be incomplete.
Edge cases: month ends and leap days
Because DATEDIF compares day numbers, dates near the end of a month give results that can surprise you.
=DATEDIF(DATE(2024,1,31),DATE(2024,2,29),"M")This returns 0. The end date is the last day of February, but its day number (29) is less than the start's (31), so Excel treats the first month as incomplete.
Compare that with EDATE:
=EDATE(DATE(2024,1,31),1)EDATE returns 29 February 2024, because it moves back to the last valid day of the month. So EDATE says one month after 31 January is 29 February, while DATEDIF says no complete month has passed by 29 February. The two functions use different rules and are not exact inverses. If a billing or contract rule treats the last day of the month as one full month, DATEDIF will undercount, so check it against the rule you actually need.
Leap days behave the same way:
=DATEDIF(DATE(2020,2,29),DATE(2021,2,28),"Y")
=DATEDIF(DATE(2020,2,29),DATE(2021,3,1),"Y")The first returns 0 and the second returns 1. Someone born on 29 February is counted as one year older on 1 March in non-leap years, not on 28 February.
Practical checks
When a DATEDIF result looks wrong, work through this list:
- Confirm both cells are real dates. Change the format to General: you should see numbers around 40000 to 50000, not left-aligned text.
- Confirm the start date is earlier than the end date.
- Confirm the unit is in straight double quotes and spelled as one of
"Y","M","D","YM","YD", or"MD". - Check whether the dates fall on 29, 30, or 31, where the day comparison matters.
- Check for time values.
DATEDIFignores the fraction, so 9 AM and 5 PM on the same date count as the same day.
Summary
DATEDIF(start, end, unit)counts complete periods between two dates."Y","M", and"D"give totals."YM"and"YD"give remainders after complete years.- The function is undocumented in the formula list but works. It returns
#NUM!when the start is later than the end. - It compares day numbers, so month-end and leap-day dates can return results that differ from
EDATEor from your own rule. - Use
"MD"with caution. The next chapter explains why.
Exercise
A2 holds 20 August 2021 and B2 holds 10 February 2024. What do =DATEDIF(A2,B2,"Y"), =DATEDIF(A2,B2,"M"), and =DATEDIF(A2,B2,"YM") return, and what text would the age formula show?
Show answer
"Y" returns 2, because 20 Aug 2023 has passed but 20 Aug 2024 has not. "M" returns 29: 20 Aug 2021 to 20 Jan 2024 is 29 months, and 10 Feb is before 20 Feb, so the next month is incomplete. "YM" returns 5, which is 29 minus 24 months in the 2 complete years. The text is "2 years, 5 months".
Exercise
EDATE(DATE(2024,1,31),1) returns 29 February 2024, but DATEDIF(DATE(2024,1,31),DATE(2024,2,29),"M") returns 0. Why do they disagree?
Show answer
EDATE clamps to the last day of the target month when the same day number does not exist. DATEDIF compares day numbers: 29 is smaller than 31, so the month is not yet complete. The functions follow different rules, so one is not the inverse of the other at month ends.