The DATEDIF function in Excel calculates the difference between two dates in a variety of units, such as years, months, or days. Its primary syntax is =DATEDIF(start_date, end_date, "unit"). This function is unique because it is a "hidden" function; it does not appear in the formula autocomplete list when you start typing in a cell, but it is fully functional across all modern versions of Excel.
What You'll Need
- A computer with Microsoft Excel installed.
- A dataset containing at least two dates (e.g., a birth date and a current date).
- Dates formatted correctly as Date cells (not as plain text) to ensure the formula recognizes the values.
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). For example, put
01/01/2020in A2 and05/15/2023in B2. Ensure these cells are formatted as "Date" via the Home tab in the Number group. - Select the Output Cell: Click on the empty cell where you want the result to appear (e.g., C2).
- Begin the Formula: Type
=DATEDIF(. Remember that Excel will not suggest this function as you type, so you must enter the spelling exactly. - Define the Start Date: Click on the cell containing the earlier date (A2) or type the date inside quotation marks (e.g.,
"1/1/2020"). Follow this with a comma. - Define the End Date: Click on the cell containing the later date (B2) or type the date inside quotation marks. Follow this with another comma.
- Choose Your Unit: This is the most critical part of the syntax. You must enter a specific code inside double quotation marks to tell Excel how to measure the difference. Choose one of the following:
"Y": Returns the total number of full years between the dates."M": Returns the total number of full months between the dates."D": Returns the total number of days between the dates."MD": Returns the difference between the days in start_date and end_date, ignoring months and years."YM": Returns the difference between the months in start_date and end_date, ignoring days and years."YD": Returns the difference between the days of start_date and end_date, ignoring years.
- Close the Formula: Type the closing parenthesis
)and press Enter. For example, a full formula to find total years would look like=DATEDIF(A2, B2, "Y"). - Verify the Result: Check that the number returned matches your expectation. If you used "Y", the result should be a whole number representing completed years.
Common Mistakes
One of the most frequent errors is reversing the start and end dates. DATEDIF requires the start_date to be chronologically earlier than the end_date. If the start date is later than the end date, Excel will return a #NUM! error. This is a common occurrence when users try to calculate age by putting the current date first and the birth date second.
Another common mistake is forgetting the double quotation marks around the unit code. Because the unit (Y, M, D, etc.) is treated as a text string, typing =DATEDIF(A2, B2, Y) without quotes will result in a #NAME? error, as Excel thinks "Y" is a named range or a separate function rather than a parameter.
Users also frequently confuse the "MD", "YM", and "YD" arguments. These are not for calculating totals, but for calculating the remainder. For instance, if you want to say someone is "3 years, 2 months, and 5 days old," you cannot do this with a single DATEDIF function. You must use three separate DATEDIF functions: one with "Y" for the years, one with "YM" for the remaining months, and one with "MD" for the remaining days.
Tips
To create a dynamic age calculator that updates every time you open the spreadsheet, replace the end_date argument with the TODAY() function. The syntax would be =DATEDIF(A2, TODAY(), "Y"). This ensures that as time passes, the calculated age increases automatically without manual data entry.
If you need to combine the results into a readable sentence, use the concatenation operator (ampersand). For example: =DATEDIF(A2, B2, "Y") & " Years, " & DATEDIF(A2, B2, "YM") & " Months". This will output a result like "3 Years, 4 Months," making the data much more professional and easier to read for stakeholders.
When dealing with large datasets, avoid typing dates directly into the formula. Instead, reference cells. This allows you to use the Fill Handle (the small square at the bottom-right of the cell) to drag the formula down and apply it to hundreds of rows instantly, which is far more efficient than editing individual formulas.
Troubleshooting
If your formula is returning a #NUM! error, first check the chronological order of your dates. Ensure the first date in the formula is the oldest. If you are using cell references, double-check that the date in the first cell is actually earlier than the date in the second cell.
If you see a #VALUE! error, this usually indicates that one of the cells you are referencing is not actually a date. Excel stores dates as serial numbers; if a date is entered as text (e.g., "Jan 1st 2020" instead of "1/1/2020"), the formula cannot perform the math. To fix this, select the column, go to the Data tab, select Text to Columns, and finish the wizard using the Date format (MDY or DMY) to convert the text into real Excel dates.
If the result looks like a weird date (e.g., "1/05/1900") instead of a number (e.g., "5"), it is because Excel has automatically applied Date formatting to the result cell. This happens because the formula references other date cells. To fix this, go to the Home tab, find the Number format dropdown, and change it from "Date" to "General" or "Number".
Frequently Asked Questions
Why doesn't DATEDIF appear in the Excel function list?
DATEDIF is a legacy function maintained for compatibility with Lotus 1-2-3. While Microsoft supports it and it works in all versions, they do not include it in the autocomplete list or the Insert Function dialog. You must type it manually.
Can I use DATEDIF to calculate workdays only?
No, DATEDIF calculates the total calendar difference. To calculate workdays (excluding weekends and holidays), you should use the NETWORKDAYS or NETWORKDAYS.INTL functions instead.
What is the difference between "M" and "YM"?
The "M" unit calculates the total number of months between two dates regardless of years. For example, 2 years and 3 months would return "27". The "YM" unit ignores the years and only returns the remaining months, which in the same example would be "3".
Does DATEDIF work in Google Sheets?
Yes, the syntax is identical in Google Sheets. You can use =DATEDIF(start_date, end_date, "unit") exactly as you would in Excel, and it will yield the same results.
How do I calculate the difference in weeks?
DATEDIF does not have a specific "W" unit for weeks. To find the number of weeks, use the "D" unit to get the total days and then divide the result by 7. The formula would be =DATEDIF(A2, B2, "D") / 7.
Will DATEDIF account for leap years?
Yes, DATEDIF is designed to handle the calendar accurately, including leap years. When using the "Y" unit, it only increments the count once the actual anniversary date of the start date has been reached in the end year.