DATEDIF is Excel’s best-kept secret. It is not listed in the function wizard, it does not autocomplete, and it has no tooltip — yet it is the only built-in way to get complete years, months, and days between two dates in a single function family. Everyone who has ever calculated an age or a tenure in Excel has eventually stumbled onto it.
This guide covers the full syntax, all six unit codes, five real-world example
workbooks (age, tenure, project duration, warranty countdown, subscription cycle),
the alternatives you should consider instead, the notorious "MD"
bug, and a 5-question exam to test yourself.
DATEDIF returns the difference between two dates in complete years, months, or days, written as =DATEDIF(start_date, end_date, unit).
What Is the DATEDIF Function?
DATEDIF measures the interval between two dates. Unlike a simple subtraction (=B2-A2), which returns the raw day count, DATEDIF can return the interval expressed in complete years, complete months, or days — and it can return the remaining months or days after whole years have been removed. That last part is why it exists.
The function dates back to Lotus 1-2-3. Microsoft kept it in Excel for spreadsheet compatibility but never documented it in the user interface. That is why you cannot find it by typing =DATE and looking through the dropdown — you have to know the name and type it in full.
A simple age, drawn out
Take the formula =DATEDIF(A2, TODAY(), "Y"). Excel reads it in three steps: check the start date, check the end date, then count how many complete years sit between them.
| A Birthdate | B Age (Years) | C Formula in B2 | |
|---|---|---|---|
| 2 | 1990-05-15 | 34 | =DATEDIF(A2,TODAY(),”Y”) |
| 3 | 2001-11-03 | 23 | =DATEDIF(A3,TODAY(),”Y”) |
| 4 | 1985-02-20 | 40 | =DATEDIF(A4,TODAY(),”Y”) |
DATEDIF is a compatibility holdover. Microsoft keeps it working because removing it would break millions of legacy spreadsheets, but they do not promote it because the "MD" unit has known accuracy issues and they would prefer you use YEARFRAC or DAYS.
Syntax and Arguments
All three arguments are required. There is no optional third argument, and there is no fourth argument for time.
=DATEDIF(start_date, end_date, unit)
| Argument | Required? | What it does |
|---|---|---|
start_date | Required | The earlier date. Can be a date literal in quotes ("1990-05-15"), a cell reference (A2), or a function such as TODAY() or DATE(1990,5,15). |
end_date | Required | The later date. Must be on or after start_date or DATEDIF returns #NUM!. |
unit | Required | A text code that selects the unit: "Y", "M", "D", "YM", "MD", or "YD". Case does not matter, but uppercase is conventional. |
Writing =DATEDIF(A2, TODAY(), Y) makes Excel look for a defined name called Y. It fails with #NAME?. Always wrap the unit code in double quotes.
Unlike subtraction, DATEDIF does not accept negative intervals. If start_date is later than end_date, the formula stops with #NUM!. This is the single most common DATEDIF error.
The Six Unit Codes
The unit code is what makes DATEDIF flexible. Each code tells the function how to slice the interval between the two dates.
| Unit | Returns | Typical use |
|---|---|---|
"Y" | Complete years between the dates | Age, employee tenure in whole years |
"M" | Complete months between the dates | Total months of a subscription or loan |
"D" | Total days between the dates | Simple day counts, project length |
"YM" | Months left over after whole years | Age in “X years, Y months” format |
"MD" | Days left over after whole months and years | Age in “X years, Y months, Z days” — use with care |
"YD" | Days left over after whole years | Day-of-year count between dates with same month/day |
Worked example — all six at once
Start date 1990-05-15, end date 2025-03-10. Here is what every unit returns:
| A Unit | B Formula | C Result | |
|---|---|---|---|
| 1 | “Y” | =DATEDIF(“1990-05-15″,”2025-03-10″,”Y”) | 34 |
| 2 | “M” | =DATEDIF(“1990-05-15″,”2025-03-10″,”M”) | 417 |
| 3 | “D” | =DATEDIF(“1990-05-15″,”2025-03-10″,”D”) | 12,718 |
| 4 | “YM” | =DATEDIF(“1990-05-15″,”2025-03-10″,”YM”) | 9 |
| 5 | “MD” | =DATEDIF(“1990-05-15″,”2025-03-10″,”MD”) | 23 (fragile) |
| 6 | “YD” | =DATEDIF(“1990-05-15″,”2025-03-10″,”YD”) | 299 (fragile) |
"MD" and "YD" units are marked fragile because they can give different answers on different Excel builds.Microsoft’s own documentation states that the "MD" argument “may result in a negative number, a zero, or an inaccurate result”. Do not use it for payroll, billing, or compliance calculations. Build the day remainder yourself from "YM" and the calendar if you need accuracy.
Real-World Examples
These are the patterns that appear in real workbooks. Each example includes the spreadsheet layout and the exact formula for each cell.
Example 1 — Age in years, months, and days
The classic use: calculate an age today from a birthdate, and format it as “X years, Y months, Z days”.
| A Name | B Birthdate | C Years | D Months | E Days | F Age string | |
|---|---|---|---|---|---|---|
| 2 | Sara Ali | 1990-05-15 | 34 | 9 | 23 | 34 years, 9 months, 23 days |
| 3 | Ravi Kumar | 2001-11-03 | 23 | 4 | 7 | 23 years, 4 months, 7 days |
| 4 | Lena Fischer | 1985-02-20 | 40 | 0 | 18 | 40 years, 0 months, 18 days |
=DATEDIF(B2,TODAY(),"Y") · D2 → =DATEDIF(B2,TODAY(),"YM") · E2 → =DATEDIF(B2,TODAY(),"MD") · F2 → =C2&" years, "&D2&" months, "&E2&" days"' Complete years
=DATEDIF(B2, TODAY(), "Y")
' Remaining months after whole years
=DATEDIF(B2, TODAY(), "YM")
' Remaining days (fragile — see note)
=DATEDIF(B2, TODAY(), "MD")
' All three, concatenated into one string
=DATEDIF(B2,TODAY(),"Y") & " years, "
& DATEDIF(B2,TODAY(),"YM") & " months, "
& DATEDIF(B2,TODAY(),"MD") & " days"
Example 2 — Employee tenure report
HR wants tenure broken into years and months, with a “milestone” flag for anyone past 5 years.
| A Employee | B Start Date | C Years | D Months | E Tenure | F Milestone | |
|---|---|---|---|---|---|---|
| 2 | Priya Shah | 2019-03-11 | 6 | 0 | 6 years, 0 months | 5+ yrs |
| 3 | Marcus Lee | 2022-08-01 | 3 | 7 | 3 years, 7 months | — |
| 4 | Elena Novak | 2020-01-15 | 6 | 2 | 6 years, 2 months | 5+ yrs |
=DATEDIF(B2,TODAY(),"Y") · D2 → =DATEDIF(B2,TODAY(),"YM") · E2 → =C2&" years, "&D2&" months" · F2 → =IF(C2>=5,"5+ yrs","—")Example 3 — Project duration tracker
Project managers want to know how long each project ran, in total days and in months.
| A Project | B Start | C End | D Days | E Months | F Status | |
|---|---|---|---|---|---|---|
| 2 | Website Redesign | 2025-01-06 | 2025-04-30 | 114 | 3 | Complete |
| 3 | CRM Migration | 2025-02-17 | 2026-01-31 | 348 | 11 | Complete |
| 4 | Data Warehouse | 2025-09-01 | 2026-06-30 | 302 | 9 | In progress |
=DATEDIF(B2,C2,"D") · E2 → =DATEDIF(B2,C2,"M") · F2 → =IF(C2<TODAY(),"Complete","In progress")Example 4 — Warranty countdown
Retail wants to know how many days of warranty remain on each product sold.
| A Product | B Sold | C Warranty (mo) | D Expires | E Days left | F Status | |
|---|---|---|---|---|---|---|
| 2 | Laptop Pro 14 | 2025-03-10 | 24 | 2027-03-10 | 365 | Active |
| 3 | Monitor 27″ | 2024-06-15 | 12 | 2025-06-15 | -97 | Expired |
| 4 | Keyboard MX | 2025-08-01 | 36 | 2028-08-01 | 874 | Active |
=EDATE(B2,C2) · E2 → =IF(D2>=TODAY(),DATEDIF(TODAY(),D2,"D"),-DATEDIF(D2,TODAY(),"D")) · F2 → =IF(D2>=TODAY(),"Active","Expired")DATEDIF refuses to count backwards, so wrap it in an IF that swaps the dates when the target has already passed. That produces a signed day count that works in both directions.
Example 5 — Subscription billing cycles
SaaS finance teams count how many complete billing cycles have elapsed since a customer signed up.
| A Customer | B Signed Up | C Months | D Cycles Billed | E Next Invoice | |
|---|---|---|---|---|---|
| 2 | Acme Corp | 2023-04-01 | 26 | 26 | 2025-06-01 |
| 3 | Globex Inc | 2024-11-15 | 7 | 7 | 2025-06-15 |
| 4 | Initech | 2025-01-01 | 5 | 5 | 2025-06-01 |
=DATEDIF(B2,TODAY(),"M") · D2 → =C2 · E2 → =EDATE(B2,C2+1)DATEDIF vs Other Date Functions
DATEDIF is not the only way to measure a date interval. Four other functions cover overlapping ground, and one of them may be a better fit for your case.
| Function | Returns | Use when |
|---|---|---|
DATEDIF | Complete years, months, or days | You need “X years, Y months, Z days” or any complete-unit count |
YEARFRAC | Fractional years (e.g. 2.75) | You need a decimal year for prorating interest or amortization |
DAYS | Plain day count | You just want the number of days — simpler and more reliable than "D" |
NETWORKDAYS | Business days only | You need to exclude weekends and holidays |
EDATE | A date n months away | You need to shift a date forward or backward by whole months |
=DATEDIF(A2,B2,"D")#NUM! if B2 is earlier than A2.=DAYS(B2,A2)' Total days — use DAYS instead of DATEDIF "D"
=DAYS(B2, A2)
' Total months — no equivalent, keep DATEDIF "M"
=DATEDIF(A2, B2, "M")
' Complete years — no equivalent, keep DATEDIF "Y"
=DATEDIF(A2, B2, "Y")
' Fractional years — use YEARFRAC for decimals
=YEARFRAC(A2, B2)
' Business days only — use NETWORKDAYS
=NETWORKDAYS(A2, B2)
YEARFRAC("1990-05-15","2025-03-10") returns roughly 34.82, not 34. If you need whole years of age, use DATEDIF. If you need the fraction of a year for a financial calculation, use YEARFRAC.
Common Errors and How to Fix Them
| Error | What it means | Fix |
|---|---|---|
#NUM! | start_date is later than end_date | Swap the arguments or guard with IF |
#NAME? | Unit code is unquoted, or the function name is misspelled | Write "Y", not Y |
#VALUE! | One of the dates is text that Excel cannot parse | Convert with DATEVALUE or DATE |
| Wrong result, no error | Using "MD" or "YD" | Avoid those units, or verify against a manual count |
| Formula does not autocomplete | DATEDIF is not in the function wizard | Type the full name — this is expected |
' Guard against reversed dates
=IF(B2>=A2, DATEDIF(A2,B2,"Y"), "Check dates")
' Wrap in IFERROR for imported data
=IFERROR(DATEDIF(A2,B2,"M"), 0)
' Convert text dates before using DATEDIF
=DATEDIF(DATEVALUE(A2), DATEVALUE(B2), "D")
' Build dates from parts
=DATEDIF(DATE(1990,5,15), TODAY(), "Y")
Practice Exam — 5 Questions
Type your answer in the box and click Check. Answers are case-insensitive and ignore extra spaces.
-
Q1. What does
=DATEDIF("1990-05-15","2025-03-10","Y")return? -
Q2. What does
=DATEDIF("1990-05-15","2025-03-10","M")return? -
Q3. What does
=DATEDIF("1990-05-15","2025-03-10","YM")return? -
Q4. What error does
=DATEDIF("2025-03-10","1990-05-15","Y")return? (Include the error symbol.) -
Q5. Which unit code returns the number of days between two dates ignoring years? (Answer with the two-letter code.)
Work through the worked example tables above — every answer is derivable from the six-unit table and the error reference.
Best Practices
DATEDIF is easy to use badly. These habits keep your date formulas correct and readable.
Check the order of dates
Always confirm start_date <= end_date, or guard with IF. Reversed dates return #NUM!.
Avoid the “MD” unit
Microsoft documents it as potentially inaccurate. Build the day remainder another way if accuracy matters.
Prefer DAYS for total days
=DAYS(end,start) is documented, handles negatives, and is easier to audit than DATEDIF(...,"D").
Use YEARFRAC for decimals
For prorated interest or amortization, YEARFRAC is the right tool, not DATEDIF.
Store dates as real dates
A text “1990-05-15” causes #VALUE!. Convert with DATEVALUE or use DATE.
Wrap with IFERROR on imports
Imported CSVs often contain blanks and text in date columns. IFERROR keeps the report clean.
Use TODAY() for live values
For “age as of today” reports, TODAY() is the right end date — but note it recalculates every time the file opens.
Document the units in the header
“Years”, “Months”, “Days” above each column makes the report readable and prevents confusion with total months.
Frequently Asked Questions
What is the DATEDIF function in Excel?
DATEDIF returns the difference between two dates in complete years, months, or days. Its syntax is =DATEDIF(start_date, end_date, unit). It is a hidden compatibility function carried over from Lotus 1-2-3.
Why doesn’t DATEDIF appear in Excel’s autocomplete?
Microsoft keeps DATEDIF for backwards compatibility but never added it to the function wizard or autocomplete list. You have to type the full name from memory.
What units does DATEDIF accept?
Six units: "Y" (complete years), "M" (complete months), "D" (days), "YM" (months ignoring years), "MD" (days ignoring months and years), and "YD" (days ignoring years).
Why does DATEDIF return #NUM!?
#NUM! appears when start_date is later than end_date. DATEDIF only accepts intervals going forward in time. Swap the arguments or guard with IF.
Is DATEDIF reliable?
Mostly yes, with one exception: the "MD" unit is documented by Microsoft as potentially inaccurate and should be avoided for anything mission-critical. The "Y", "M", "D", and "YM" units are reliable.
How do I calculate age in Excel?
Use =DATEDIF(birthdate, TODAY(), "Y") to get complete years of age. For “X years, Y months, Z days”, concatenate three DATEDIF calls with different units.
How do I get years, months, and days in one cell?
Concatenate three DATEDIF calls: =DATEDIF(A2,TODAY(),"Y")&" years, "&DATEDIF(A2,TODAY(),"YM")&" months, "&DATEDIF(A2,TODAY(),"MD")&" days". Note the "MD" caveat above.
What replaced DATEDIF?
Nothing replaced it. YEARFRAC, DAYS, and NETWORKDAYS handle related tasks, but no other function produces the same complete-unit output that "Y", "M", and "YM" produce.
Does DATEDIF work with times?
No. DATEDIF ignores the time component entirely and works only with the date portion. For time differences, subtract the two values directly and format the result as [h]:mm.
Is DATEDIF case-sensitive?
No. "y", "Y", "d", and "D" all work identically. Uppercase is the convention for readability.
Key Takeaways
- DATEDIF returns the interval between two dates in complete units.
- The syntax is
=DATEDIF(start_date, end_date, unit)— all three arguments are required. - Six units:
"Y","M","D","YM","MD","YD". - It is a hidden function — no autocomplete, no tooltip, no wizard entry.
- Reversed dates return
#NUM!; guard withIF. - The
"MD"unit is documented as unreliable — avoid it for anything important. - Use
DAYSinstead of"D"for a plain total day count. - Use
YEARFRACinstead of DATEDIF when you need fractional years. - Dates must be real date values, not text — otherwise
#VALUE!. - For live age reports,
TODAY()as the end date recalculates on every open. - Chain
"Y","YM", and"MD"to build an “X years, Y months, Z days” string. - Wrap DATEDIF in
IFERRORwhen working with imported or user-entered dates.
What to Learn Next
DATEDIF is one member of Excel’s date and logical family. The natural next steps are the functions that pair with it in real workbooks.
Start with the companion date functions: yearfrac-function, days-function, and networkdays-function.
Then connect them to the logical tools: if-function, iferror-function, and ifs-function.