To get the DATEDIF function in Excel, you simply start typing =DATEDIF( into a cell. Unlike most Excel functions, DATEDIF is a "hidden" or undocumented function; it will not appear in the Formula AutoComplete dropdown list and there is no built-in help tooltip to guide you through the arguments. Despite its invisibility, it is fully functional in all modern versions of Excel and is the most efficient way to calculate the difference between two dates in years, months, or days.
What You'll Need
- A computer with Microsoft Excel installed (works on Windows and macOS).
- Two valid date entries in your spreadsheet (e.g., a start date in cell A2 and an end date in cell B2).
- Dates must be formatted as actual Date values, not as plain text.
Step-by-Step
- Prepare Your Data: Enter your start date in one cell (e.g., A2) and your end date in another cell (e.g., B2). Ensure the start date is chronologically earlier than the end date to avoid errors.
- Select the Target Cell: Click on the empty cell where you want the result of the date calculation to appear.
- Enter the Function Start: Type
=DATEDIF(. Note that as you type, Excel will not suggest the function or show you which arguments to use. You must type the entire formula manually. - Select the Start Date: Click on the cell containing the start date (e.g., A2) or type the cell reference. Follow this with a comma.
- Select the End Date: Click on the cell containing the end date (e.g., B2) or type the cell reference. Follow this with another comma.
- Define the Interval Unit: Now you must tell Excel how you want the difference measured. Enclose the unit code in double quotation marks. Common codes include:
"Y": Returns the total number of complete years between the dates."M": Returns the total number of complete months between the dates."D": Returns the total number of days between the dates."YM": Returns the number of months remaining after subtracting the full years."MD": Returns the number of days remaining after subtracting full months."YD": Returns the number of days remaining after subtracting full years.
- Close and Execute: Type the closing parenthesis
)and press Enter. For example, a full formula to find the age of a person would look like:=DATEDIF(A2, B2, "Y").
Common Mistakes
The most frequent error users encounter is the #NUM! error. This happens when the start date is later than the end date. DATEDIF cannot calculate negative time intervals; it requires the first date argument to be the oldest and the second to be the newest. If your data is sorted inconsistently, you will see this error across your column.
Another common mistake is forgetting the double quotation marks around the unit code. If you type =DATEDIF(A2, B2, Y) instead of =DATEDIF(A2, B2, "Y"), Excel will treat the letter Y as a named range or a variable. Since Y is likely not defined in your workbook, Excel will return a #NAME? error.
Users also often mistake DATEDIF for a standard subtraction formula. While =B2-A2 gives you the total number of days, it cannot easily provide the number of complete months or years without complex division (like dividing by 365.25), which often leads to rounding inaccuracies. DATEDIF is specifically designed to handle the varying lengths of months and leap years automatically.
Tips
To create a human-readable age or tenure string (e.g., "5 Years, 2 Months, 11 Days"), you can combine multiple DATEDIF functions using the ampersand (&) symbol for concatenation. For example, use this formula structure:
=DATEDIF(A2, B2, "Y") & " Years, " & DATEDIF(A2, B2, "YM") & " Months, " & DATEDIF(A2, B2, "MD") & " Days".
If you want the calculation to always be relative to today's date, replace the end date cell reference with the TODAY() function. For example, =DATEDIF(A2, TODAY(), "Y") will automatically update the age or tenure every time you open the spreadsheet based on the current system clock.
When working with large datasets, ensure your date columns are explicitly formatted as "Date" via the Home tab > Number Format dropdown. If Excel perceives your dates as text, the DATEDIF function will return a #VALUE! error because it cannot perform mathematical operations on text strings.
Troubleshooting
If the function is not working as expected, follow these steps in order:
- Check for #VALUE! Errors: This usually means one of your dates is not actually a date. Select the cell and look at the Formula Bar. If the date is aligned to the left of the cell by default, Excel thinks it is text. Try re-entering the date or using the "Text to Columns" tool under the Data tab to convert the column to Date format.
- Check for #NUM! Errors: Verify that the date in the first argument is earlier than the date in the second argument. If you are calculating the difference between a fixed date and a variable date that might be in the past, you may need to use an IF statement to swap the arguments.
- Check for #NAME? Errors: Ensure you have spelled the function exactly as
DATEDIFand that your unit codes (Y, M, D, etc.) are wrapped in double quotes. - Verify Regional Date Settings: If you are using a date format like DD/MM/YYYY but your system is set to MM/DD/YYYY, Excel may misinterpret the date or fail to recognize it entirely. Check your system's regional settings in the Control Panel or System Settings to ensure they match your data entry style.
Frequently Asked Questions
Why isn't DATEDIF showing up in the Excel function list?
DATEDIF is a legacy function originally included for compatibility with Lotus 1-2-3. Microsoft does not officially document it in the current version's function library or autocomplete list, but it remains available for all users to type manually.
Is DATEDIF available in Google Sheets?
Yes, DATEDIF works in Google Sheets using the exact same syntax and unit codes as it does in Microsoft Excel.
What is the difference between "M" and "YM"?
The "M" unit calculates the total number of full months between two dates regardless of how many years have passed. The "YM" unit ignores the years and only tells you how many months are left over (a value between 0 and 11).
How do I handle leap years with DATEDIF?
You do not need to do anything. DATEDIF is designed to account for leap years automatically when calculating "Y" (years) and "D" (days), ensuring the result is calendar-accurate.
Can I use DATEDIF to find the number of workdays?
No, DATEDIF calculates total calendar time. To find the number of business days excluding weekends and holidays, you should use the NETWORKDAYS or NETWORKDAYS.INTL function instead.