Skip to content

DATEDIF

Counts the complete years, months or days between two dates. It works in every version of Excel but is missing from the function list and autocomplete.

Syntax

=DATEDIF(, , )

Date and time

Works in all versions of Excel, but isn't listed in the Insert Function dialog, and the formula bar doesn't suggest it as you type.

Try it

Edit the formula in C2 or change the inputs. The sheet recalculates as you type.

C2
A
B
C
1
Birth date
As of
Age
2
20-Jun-1990
15-Mar-2025
34
3

C2 =DATEDIF(A2,B2,"y") returns 34

How DATEDIF works

DATEDIF returns the number of complete years, months or days between two dates. It is the standard way to calculate age and length of service: =DATEDIF(A2,TODAY(),"y") gives the complete years since a birth date in A2.

It works, but it is undocumented in the function list

DATEDIF came into Excel for compatibility with Lotus 1-2-3. It does not appear in the Insert Function dialog, and Excel doesn't suggest it as you type, so you have to know the name and spell it out. It works in every current version, including Microsoft 365 and Excel for the web. Microsoft keeps only a short support page for it that warns about one of its units, covered below.

The unit argument

The unit is text in quotes, and it decides what is counted:

  • "y" counts complete years.
  • "m" counts complete months.
  • "d" counts days, the same as subtracting the dates.
  • "ym" counts the months left after removing complete years, from 0 to 11.
  • "yd" counts the days left after removing complete years.
  • "md" counts the days left after removing complete months.

"Complete" means a year or month isn't counted until it has fully elapsed. From 20 June 1990 to 15 March 2025 there are 34 complete years, because the 35th birthday hasn't arrived, and 416 complete months.

An age in years, months and days

Chain three DATEDIFs with text: =DATEDIF(A2,B2,"y")&" years, "&DATEDIF(A2,B2,"ym")&" months". For the same dates that returns "34 years, 8 months". This is the usual use of the "ym" unit.

The "md" problem

Be careful with "md". It returns the days left over after complete months, and when the end date's day is earlier than the start date's day, Excel borrows from a month in a way that can give a wrong answer, zero or even a negative number. Microsoft's own support page lists it as unreliable. Don't use "md" in a model that matters. Work out leftover days yourself instead: subtract the date reached by EDATE, as in =B2-EDATE(A2,DATEDIF(A2,B2,"m")).

Errors

If start_date is later than end_date, you get #NUM!. A misspelt unit, or a missing pair of quotes, also gives #NUM! or #NAME?. Text that isn't a date gives #VALUE!.

Alternatives

YEARFRAC returns a decimal number of years. Subtracting dates gives days. NETWORKDAYS counts working days. DATEDIF is the only built-in function that counts whole months and years in one step.

Arguments

start_date
The earlier date. If it is later than end_date, DATEDIF returns #NUM!.
end_date
The later date.
unit
A text code, in quotes, for what to count. "y": complete years. "m": complete months. "d": days. "ym": months left over after the complete years. "yd": days left over after the complete years. "md": days left over after the complete months, which is unreliable (see the notes below). Any other code returns #NUM!.

Examples

Each example is a live sheet. Change the inputs or the formula and watch the result.

Example 1

Age in complete years

C2 =DATEDIF(A2,B2,"y")

Result: 34

The 35th birthday on 20 June 2025 hasn't happened yet, so only 34 years are complete. In real use, replace B2 with TODAY().

C2
A
B
C
1
Birth date
As of
Age
2
20-Jun-1990
15-Mar-2025
34
3

C2 =DATEDIF(A2,B2,"y") returns 34

Example 2

Years and months of service

C2 =DATEDIF(A2,B2,"y")&" years, "&DATEDIF(A2,B2,"ym")&" months"

Result: 34 years, 8 months

"y" gives the 34 complete years and "ym" gives the 8 months left after those years. Joining them with & builds the text.

C2
A
B
C
1
Start date
As of
Service
2
20-Jun-1990
15-Mar-2025
34 years, 8 months
3

C2 =DATEDIF(A2,B2,"y")&" years, "&DATEDIF(A2,B2,"ym")&" months" returns 34 years, 8 months

Example 3

Total complete months

C2 =DATEDIF(A2,B2,"m")

Result: 416

34 years is 408 months, plus 8 more complete months. That is the figure to use for a monthly subscription or loan term.

C2
A
B
C
1
Start date
As of
Months
2
20-Jun-1990
15-Mar-2025
416
3

C2 =DATEDIF(A2,B2,"m") returns 416

Example 4

Leftover days where "md" is safe

C2 =DATEDIF(A2,B2,"md")

Result: 5

From 15 January there are 2 complete months to 15 March, and 5 days left to 20 March. Here the end day (20) is later than the start day (15), so "md" behaves. It is when the end day is earlier that it can go wrong.

C2
A
B
C
1
Start date
End date
Extra days
2
15-Jan-2025
20-Mar-2025
5
3

C2 =DATEDIF(A2,B2,"md") returns 5

Your turn.

Solve it in the sheet. Every answer is checked, with hints if you need them.

Task 1 of 2

In C2, return each person's age in complete years on the "As of" date. Fill it down to C4.

Answer in C2, then it fills down to C4.

C2
A
B
C
D
E
F
1
Birth date
As of
Age (years)
Months old
2
20-Jun-1990
15-Mar-2025
3
15-Mar-1985
15-Mar-2025
4
31-Dec-2001
15-Mar-2025
5

Common mistakes

  • DATEDIF isn't in the function list or autocomplete, so there is no argument tooltip. Type the name in full, and put the unit in quotes: "y", not y.
  • The unit "md" can return a wrong, zero or negative result when the end day is earlier than the start day. Avoid it, or calculate leftover days with =B2-EDATE(A2,DATEDIF(A2,B2,"m")).
  • The start date must not be later than the end date. If it is, DATEDIF returns #NUM!. Check the order, or use MIN and MAX to put the dates in order.
  • "Complete" means rounded down. 34 years and 11 months is 34, not 35. For a decimal, use YEARFRAC.
  • Subtracting years gives the wrong age. YEAR(B2)-YEAR(A2) is 35 above, because it ignores whether the birthday has come round.

Questions people ask

Why isn't DATEDIF in the Excel function list?

It was kept for compatibility with Lotus 1-2-3 and never added to the Insert Function dialog or autocomplete. It still works if you type it in full.

How do I calculate age in Excel?

Use `=DATEDIF(A2,TODAY(),"y")` for complete years. For years and months, join `"y"` and `"ym"` as text.

Why does DATEDIF return #NUM!?

Usually the start date is later than the end date. A unit code that isn't one of the six also gives `#NUM!`.

Is the "md" unit reliable?

Not when the end day is earlier than the start day. It can return a wrong, zero or negative number. Calculate leftover days with EDATE instead.

What is the difference between DATEDIF and YEARFRAC?

DATEDIF counts whole years or months and drops the remainder. YEARFRAC returns a decimal, such as 34.74, and lets you choose a day-count basis.

Learn DATEDIF in a real sheet.

DATEDIF is taught in Dates and financial modelling, lesson 8: Tenure and age with DATEDIF and YEARFRAC. Each lesson is a short, graded exercise like the one above.