Sub topic 11
DATEDIF: counting complete years, months, and days
This chapter assumes you can enter formulas, use cell references such as A2, and know that a date is a serial number (a count of days) formatted to look like a date. Earlier chapters covered subtracting dates and using TODAY.
Why DATEDIF exists
Subtracting two dates gives days. Dividing by 365 to get years is wrong, because years vary in length. DATEDIF counts complete calendar units between two dates, so a person who is 29 years and 364 days old is 29, not 30.
Syntax
=DATEDIF(start_date, end_date, unit)start_dateis the earlier date.end_dateis the later date.unitis text in quotes that selects what to count.
This chapter uses three units. The next chapter covers the others, including why MD is unreliable.
| Unit | Returns |
|---|---|
| "Y" | Complete years between the dates |
| "M" | Complete months between the dates |
| "D" | Days between the dates, the same as subtracting |
A worked example
Take a start date of 15 January 2020 and an end date of 14 March 2023.
=DATEDIF(DATE(2020,1,15),DATE(2023,3,14),"Y")This returns 3. The third anniversary, 15 January 2023, has passed. The fourth has not.
=DATEDIF(DATE(2020,1,15),DATE(2023,3,14),"M")This returns 37. Thirty-six months end on 15 January 2023, and the 37th ends on 15 February 2023. The 38th would end on 15 March 2023, which is one day too late.
=DATEDIF(DATE(2020,1,15),DATE(2023,3,14),"D")This returns 1154, the same as =DATE(2023,3,14)-DATE(2020,1,15).
Using cell references
With a hire date in A2 and a reference date in B2, this returns complete years of service:
=DATEDIF(A2,B2,"Y")Replace B2 with TODAY() to get a value that updates each time the workbook recalculates.
Common errors
- #NUM! appears when
start_dateis later thanend_date. Swap the arguments, or wrap the call with a check such as=IF(A2>B2,"",DATEDIF(A2,B2,"Y")). - #NUM! also appears for an unrecognized unit.
"Years"is not valid. Use"Y". - #VALUE! appears when an argument is not a real date, for example a date typed as text. A later chapter covers this.
- #NAME? means the function name is misspelled.
Y versus a day-count estimate
A day-count estimate can be off by one near a birthday. This formula divides by 365.25.
=INT((B2-A2)/365.25)It is usually right, but it can fail on the day of an anniversary. DATEDIF(A2,B2,"Y") compares calendar dates directly, so it does not drift.
Exercise
What do =DATEDIF(DATE(2021,5,10),DATE(2023,5,9),"Y") and =DATEDIF(DATE(2021,5,10),DATE(2023,5,9),"M") return, and why?
Show answer
They return 1 and 23. The second anniversary, 10 May 2023, is one day after the end date, so only 1 full year has passed. Twenty-three monthly anniversaries have passed, and the 24th falls on 10 May 2023, after the end date.
Exercise
A2 holds 1 March 2024 and B2 holds 1 January 2024. What does =DATEDIF(A2,B2,"D") return?
Show answer
#NUM!, because the start date is later than the end date. Swap the arguments, or use =B2-A2 to get a negative day count.