Sub topic 10
Measuring Elapsed Years and Months
This chapter assumes you know that dates are stored as serial numbers, that subtracting dates gives days, and that TODAY() and DATE() return date values. It builds on the earlier chapters on date arithmetic and on extracting the parts of a date.
Why subtraction is not enough
Subtracting two dates gives days. Years and months have different lengths, so you cannot turn a day count into "complete years" or "complete months" by dividing by a constant. Dividing by 365 drifts around leap years, and dividing by 30 drifts every month. Sheets has two functions for this job: DATEDIF counts complete units, and YEARFRAC returns a fractional number of years.
DATEDIF: complete units between two dates
The syntax is DATEDIF(start_date, end_date, unit). The unit is a text string, so it must be in quotes. DATEDIF counts only complete units. It does not round.
| Unit | Returns | Example result for 2020-03-15 to 2026-11-28 |
|---|---|---|
| `"Y"` | Complete years | 6 |
| `"M"` | Complete months | 80 |
| `"D"` | Days | 2449 |
| `"YM"` | Months left over after removing complete years | 8 |
| `"MD"` | Days left over after removing complete months | 13 |
| `"YD"` | Days left over after removing complete years | 258 |
Put the two dates in cells so you can try each unit. This example builds them with DATE, so the result does not depend on locale parsing:
A2: =DATE(2020, 3, 15)
B2: =DATE(2026, 11, 28)
C2: =DATEDIF(A2, B2, "Y")
D2: =DATEDIF(A2, B2, "M")
E2: =DATEDIF(A2, B2, "YM")C2 shows 6, D2 shows 80, and E2 shows 8. The 80 months are 6 years × 12 plus the 8 months from March 15 to November 15. The 13 extra days after November 15 do not make another complete month.
Ages and tenure
The most common use is an age in whole years. The end date is TODAY():
=DATEDIF(A2, TODAY(), "Y")For a label such as "6 years, 8 months", combine the "Y" and "YM" units with text:
=DATEDIF(A2, B2, "Y") & " years, " & DATEDIF(A2, B2, "YM") & " months"The result is text, not a number. Use it for display. Keep a separate numeric column if you need to sort, filter or calculate with the value.
How "complete" is decided
DATEDIF compares the day of the month. A month is complete only when the end date's day number is at least the start date's day number. From 2026-01-31 to 2026-02-28, "M" returns 0, because day 28 is earlier than day 31. This differs from EDATE, which would move January 31 to February 28 by clipping to the last valid day. If you need "the same date, one month later" semantics, use EDATE and compare the results yourself.
The "MD" unit deserves caution. It is the leftover-days unit, and spreadsheet programs have long had a reputation for odd results from it. In some versions of Excel it can give wrong or negative-looking answers around month ends. If you need leftover days, calculate them from a date you build yourself:
=B2 - EDATE(A2, DATEDIF(A2, B2, "M"))This takes the start date, adds the number of complete months, and subtracts that date from the end date. For the example above, EDATE(A2, 80) is 2026-11-15, so the result is 13. This approach is explicit and easy to check.
Errors
- If
start_dateis later thanend_date,DATEDIFreturns#NUM!. Wrap it withIF(A2 > B2, ...)or order the dates withMINandMAX. - If the unit text is not one of the supported values, you get
#NUM!. A misspelled unit or a missing quote is the usual cause. - If a cell holds text that only looks like a date, you get
#VALUE!. Check it withISNUMBER(A2). The earlier chapter on converting text to dates covers the fix.
=IF(A2 > B2, "", DATEDIF(A2, B2, "Y"))Calendar months versus complete months
Sometimes you want to count calendar boundaries instead of complete months. For example, a billing report may treat January 31 to February 1 as one month. DATEDIF returns 0 here, so use this formula:
=(YEAR(B2) - YEAR(A2)) * 12 + MONTH(B2) - MONTH(A2)It ignores the day numbers completely. For 2020-03-15 to 2026-11-28 it gives 80, the same as DATEDIF. For 2026-01-31 to 2026-02-01 it gives 1, where DATEDIF gives 0. Pick the definition that matches your business rule and write it down next to the sheet.
YEARFRAC: elapsed time as a fraction of a year
YEARFRAC(start_date, end_date, [basis]) returns the time between two dates as a decimal number of years. It suits interest accruals, prorated fees and any case where 6.7 years is more useful than "6 years and 8 months". The result is positive whichever date comes first.
The optional basis sets how days and years are counted:
| Basis | Day count convention | 2020-03-15 to 2026-11-28 |
|---|---|---|
| 0 (default) | US 30/360: every month has 30 days, a year has 360 | 6.7028 |
| 1 | Actual days over the actual length of the years involved | close to 6.71 |
| 2 | Actual days over 360 | 6.8028 |
| 3 | Actual days over 365 | 6.7096 |
| 4 | European 30/360 | 6.7028 |
Here is how the values arise. Under basis 0 the day count is (2026 − 2020) × 360 + (11 − 3) × 30 + (28 − 15) = 2413, and 2413 ÷ 360 is 6.7028. Under basis 3 the actual day count is 2449, and 2449 ÷ 365 is 6.7096. Under basis 2, 2449 ÷ 360 is 6.8028.
=YEARFRAC(A2, B2)
=YEARFRAC(A2, B2, 1)
=YEARFRAC(A2, B2, 3)The default of 0 surprises many people. It is a financial convention, so a plain age calculation with the default basis does not match actual calendar days. For elapsed time based on real days, use basis 1 or 3, and say which one you chose.
Rounding the result
YEARFRAC is not a replacement for DATEDIF(..., "Y"). If you take INT(YEARFRAC(A2, B2)) to get whole years, results near an anniversary can be off by one, because 30/360 counting can reach a whole number a day early or late. Use DATEDIF for complete years and YEARFRAC only when you want the fraction.
Choosing between them
Edge cases to check
- Leap-day start dates. Someone born on 2024-02-29 has an age that increases in non-leap years on a date you must define. Test your formula with a February 29 start and an end date of March 1 and February 28 in a non-leap year. Decide what your rule is before you trust the sheet.
- Time of day. If a cell holds a date-time,
DATEDIFandYEARFRACwork from the date part. Elapsed hours need the subtraction approach from the earlier chapter. - Volatile end dates. A formula using
TODAY()changes every day. A stored age therefore changes without anyone editing the sheet. For reports that must not change, type or paste the fixed end date. - Documentation.
DATEDIFworks in Sheets but is sometimes missing from function suggestions and autocomplete. Type the full name and unit, and check the result against a case you can count by hand.
Exercises
Exercise
A2 holds 2019-08-31 and B2 holds 2026-02-28. What does =DATEDIF(A2, B2, "Y") return, and what does =DATEDIF(A2, B2, "YM") return?
Show answer
"Y" returns 6, because the seventh anniversary (2026-08-31) has not been reached. "YM" returns 5. Counting from 2019-08-31 gives 6 years at 2025-08-31, and from there to 2026-02-28 is 5 complete months, since each month's day-31 mark or the shorter month's end has to be reached: Sep 30, Oct 31, Nov 30, Dec 31 and Jan 31 are crossed, but February's 28 is earlier than 31, so it is not a sixth month.
Exercise
Write a formula that shows tenure as text such as "6 years, 8 months" for a start date in A2, using today's date as the end. Then explain why you would keep a separate numeric column.
Show answer
=DATEDIF(A2, TODAY(), "Y") & " years, " & DATEDIF(A2, TODAY(), "YM") & " months". The result is text, so it cannot be sorted by size, filtered numerically or used in arithmetic. A numeric column, for example =DATEDIF(A2, TODAY(), "M") for total months, supports those operations.
Exercise
You need the fraction of a year between 2026-01-01 and 2026-07-01, based on actual days. Which formula is appropriate and why not the default basis?
Show answer
=YEARFRAC(DATE(2026,1,1), DATE(2026,7,1), 1) or basis 3. The default basis 0 counts every month as 30 days, so it ignores the real month lengths. With basis 3 the answer is 181 ÷ 365, about 0.4959. Basis 0 gives exactly 0.5.