Sub topic 3
DATEDIF: Counting Complete Years, Months, and Days
Assume you can enter formulas, use cell references such as A2, and apply number formats. DATEDIF calculates elapsed time between a start date and an end date.
Complete elapsed units
The syntax is:
=DATEDIF(start_date,end_date,unit)For example, if A2 contains a start date and B2 contains an end date:
=DATEDIF(A2,B2,"Y")
=DATEDIF(A2,B2,"M")
=DATEDIF(A2,B2,"D")These return complete years, complete months, and total days. The "YM" unit returns leftover complete months after complete years. The "YD" unit returns days after removing complete years.
Units and the `MD` warning
Use these units deliberately:
| Unit | Meaning |
|---|---|
| `"Y"` | Complete years |
| `"M"` | Complete months |
| `"D"` | Total days |
| `"YM"` | Months left after complete years |
| `"YD"` | Days left after complete years |
| `"MD"` | Days left after complete months and years |
"MD" is unreliable around month lengths and month ends. Avoid it for reporting. Build the result from safer units, or calculate total days directly.
DATEDIF returns #NUM! when the start date is later than the end date. It also does not accept a future end date in a meaningful “elapsed” calculation unless that is intended.
> callout warning > The end date must be on or after the start date. Validate dates before displaying a result.
Practical patterns
For an age or tenure display:
=DATEDIF(A2,TODAY(),"Y")&" years, "&DATEDIF(A2,TODAY(),"YM")&" months"For a due date 30 days after a date in A2, use =A2+30. For a month-based due date, use =EDATE(A2,1). To define the current month, use =EOMONTH(TODAY(),-1)+1 for its first day and =EOMONTH(TODAY(),0) for its last day.
Text dates and common errors
Excel may store an imported date as text instead of a date serial number. Date arithmetic can then return #VALUE!, or Excel may interpret a day and month incorrectly because of regional settings. Test a value with =ISNUMBER(A2). Convert reliable text with DATEVALUE, then check the result and format it as a date.
Exercise
If A2 is 15 March 2020 and B2 is 14 March 2024, what does =DATEDIF(A2,B2,"Y") return?
Show answer
3, because four complete years have not elapsed.