Start free
Date & time

DATEDIF function

DATEDIF counts the complete years, months or days between two dates: =DATEDIF(A2,B2,"y") is an age in whole years. Excel doesn't suggest it as you type, but it works all the same.

Available in Excel 2007 and later, Excel for the web and Google Sheets

Key takeaways

  • =DATEDIF(start, end, "y") counts complete years: the usual formula for an age.
  • "m" counts complete months and "d" days; "ym" gives the months left over after the whole years.
  • The earlier date goes first, or the result is #NUM!.
  • Avoid "md": Microsoft warns it can give a negative number, a zero or a wrong count. =end-EDATE(start,DATEDIF(start,end,"m")) gives the leftover days without that problem.
On this page

DATEDIF syntax

=DATEDIF(start_date, end_date, unit)
ArgumentRequiredWhat it means
start_dateYesThe earlier date. If it's after end_date, DATEDIF returns #NUM!.
end_dateYesThe later date: often TODAY(), or a fixed date in a cell.
unitYesWhat to count, in quotes: "y" complete years, "m" complete months, "d" days; "ym" the months left after the whole years, "yd" the days left after the whole years, "md" the days left after the whole months (unreliable, see below). Capitals work too.

Examples

Age in whole years

The birthday in July hasn't come yet on 10 March 2026, so the age is still the lower number.

C2=DATEDIF(A2,B2,"y")
RowABC
1BornOnAge
222 Jul 199110 Mar 202634
=DATEDIF(A2,B2,"y") → 34

Complete months of service

"m" counts only whole months: the 16th of the month hasn't come round again by 10 March, so the month that started on 16 February doesn't count yet.

C2=DATEDIF(A2,B2,"m")
RowABC
1StartedOnMonths
216 Sept 202410 Mar 202617
=DATEDIF(A2,B2,"m") → 17

Years and months together

"y" gives the whole years and "ym" the months left over after them; joined with text, they read as a length of service.

C2=DATEDIF(A2,B2,"y")&" years, "&DATEDIF(A2,B2,"ym")&" months"
RowABC
1StartedOnService
22 May 201910 Mar 20266 years, 10 months
=DATEDIF(A2,B2,"y")&" years, "&DATEDIF(A2,B2,"ym")&" months" → 6 years, 10 months

Why "md" can't be trusted

From 31 January to 1 March is one whole month and one day, but "md" returns a negative number. It works as if 31 January plus a month were 31 February, which runs on to 3 March, two days after the end date.

C2=DATEDIF(A2,B2,"md")
RowABC
1StartEndmd
231 Jan 20261 Mar 2026-2
=DATEDIF(A2,B2,"md") → -2

The days after the last whole month, done right

DATEDIF with "m" counts the whole months, EDATE moves the start date on by that many, and what's left until the end date is the extra days.

C2=B2-EDATE(A2,DATEDIF(A2,B2,"m"))
RowABC
1StartEndDays
231 Jan 20261 Mar 20261
=B2-EDATE(A2,DATEDIF(A2,B2,"m")) → 1

Practise it

New data, so you can't copy the answer above. We check the number your formula returns, then change the data to make sure it still holds.

Your task: In C2, work out how many complete months the employee has worked, from the start date in B2 to the date in E2.

Graded live
RowABCDE
1EmployeeStartedMonthsAs of
2Lena14 Aug 202310 Mar 2026

Write the formula in the bar above, then press Check or Enter.

Hint

DATEDIF takes the start date (B2), the end date (E2) and the unit "m".

Solution

=DATEDIF(B2,E2,"m") Any formula that returns the same result passes.

Common mistakes

  1. The dates the wrong way round. The start date must be the earlier one. The other way round, DATEDIF returns #NUM!.
  2. Leaving out the quotes. The unit is text: "y", not y. Without the quotes Excel looks for a name called y and shows #NAME?.
  3. Trusting "md". Around the end of a month "md" can return a negative number or zero, as Microsoft warns. Subtract EDATE of the start date instead, as in the last example.
  4. Looking for it in the function list. Excel doesn't offer DATEDIF as you type or list it in Insert Function. Type it in full, with all three arguments, and it works.

The units, side by side

From 15 January 2023 to 10 March 2026, as Excel counts them:

Unit What it counts Result
"y" Complete years 3
"m" Complete months 37
"d" Days 1150
"ym" Months left after the whole years 1
"yd" Days left after the whole years 54
"md" Days left after the whole months 23

Here every unit gives the count you’d expect. "yd" can be a day out when a 29 February falls between the two dates’ month and day in one year but not the other, because Excel counts "yd" as if both dates were in the start date’s year. "md" goes wrong more often: whenever the end date’s day is lower than the start date’s and the month before the end date is too short to hold the start date’s day, as with a start on 29, 30 or 31 January and an end date early in March.

FAQ

How do I calculate someone's age in Excel?

=DATEDIF(A2,TODAY(),"y") with the date of birth in A2. It counts complete years only, so the age stays the lower number until this year's birthday comes round.

Why doesn't Excel show DATEDIF when I type it?

Excel keeps it for older workbooks and leaves it out of its suggestions and its function list. It still works: type it in full.

Why does DATEDIF return #NUM!?

The start date is later than the end date, or the unit isn't one DATEDIF knows. Put the earlier date first and check the unit's spelling.

Can I just subtract the dates?

=B2-A2 gives the number of days, the same as "d". For whole months or years, use DATEDIF: dividing the days by 30 or 365 drifts.

Guides that use DATEDIF

Worked problems with practice sheets.

Errors & fixesPractice sheet

#NUM! error in Excel: what it means and how to fix it

#NUM! means a calculation has no valid answer: an IRR with no rate to find, a square root of a negative. Each cause and its fix, with a practice sheet.

2 min read · Updated

Know DATEDIF? Prove it on a real sheet.

Practise DATEDIF and the functions around it in a live grid that grades your result and explains your mistakes.

Runs in your browser · Graded on your result · No install