You can calculate the number of years between two dates in Excel using the DATEDIF function, which returns the difference in days, months, or years. This function is ideal for tracking tenure, age, or project duration without needing complex nested formulas. Simply enter the start date, end date, and specify the unit as "y" for years to get a whole number result.
What You'll Need
- A computer with Microsoft Excel installed (Desktop, Web, or Mobile versions).
- Two dates in separate cells. These can be the start date and end date of a project, birth dates for age calculation, or hire and termination dates for employment records.
- Familiarity with basic Excel cell formatting (e.g., ensuring cells are set as "Date" rather than "Text").
Step-by-Step
- Prepare your spreadsheet: Open your Excel workbook and navigate to a blank sheet. Click on the first cell (A1) and type the start date. Press Enter. Click on the next cell (B1) and type the end date. Press Enter. Ensure both cells are formatted as dates by right-clicking the cell, selecting "Format Cells," choosing "Date," and clicking OK.
- Enter the DATEDIF formula: Click on a third cell (C1) where you want the result to appear. Type the equals sign (=) to start the formula. Type the function name DATEDIF. Open a parenthesis (. Click on cell A1 (start date) to reference it. Type a comma ,. Click on cell B1 (end date). Type another comma ,. Type the unit code "y" and close the parenthesis ). The final formula should look like this:
=DATEDIF(A1, B1, "y"). - Complete the calculation: Press the Enter key on your keyboard. Excel will calculate the difference in complete years between the two dates. For example, if A1 is 01/01/2018 and B1 is 15/06/2023, the result will be 5. This indicates five full years have passed.
- Handling future dates: The DATEDIF function works correctly whether the end date is in the past or the future. If the end date is earlier than the start date, Excel will return a negative number. For instance, if A1 is 15/06/2023 and B1 is 01/01/2018, the result will be -5.
- Formatting the result: By default, Excel displays the result as a standard number. If you need to display it as a decimal (e.g., 5.5 years), you can change the cell format. Right-click the result cell, select "Format Cells," choose "Number," and set the decimal places to 1 or 2. However, for most age or tenure calculations, the whole number is preferred.
Common Mistakes
One common mistake is not formatting the source cells as dates. If you type "01/01/2018" or "January 1, 2018" directly into a cell without formatting it, Excel may treat it as text. When you try to use it in a DATEDIF formula, you will see a #VALUE! error. To fix this, ensure the cells are explicitly set to the Date format.
Another frequent error is using the wrong unit code. The DATEDIF function accepts three unit codes: "y" for years, "m" for months, and "d" for days. If you accidentally type "yr", "month", or "days", the formula will return a #NUM! error. The unit code must be in double quotes.
Users often try to use the subtraction operator (-) instead of DATEDIF. For example, typing =B1-A1 will give you the difference in days, not years. To get the years, you would need to divide that result by 365, which is inaccurate due to leap years and varying month lengths. The DATEDIF function is designed specifically for this purpose and handles the calendar math correctly.
Tips
When calculating age, it is sometimes useful to show the person's age as of a specific date, such as their last birthday. The DATEDIF function with the "y" unit provides this exact information. For example, if calculating the age of someone born on 05/12/1990 and the current date is 10/06/2023, the formula will return 32, indicating they have had 32 birthdays.
If you need to calculate the total years and months (e.g., 5 years and 2 months), you can use two DATEDIF functions in separate cells and concatenate them with text. In cell C1, type =DATEDIF(A1, B1, "y") for years. In cell D1, type =DATEDIF(A1, B1, "ym") for the remaining months. In cell E1, type =C1 & " years, " & D1 & " months" to get the full description.
For dynamic calculations that always use the current date, you can replace the cell reference for the end date with the TODAY() function. For example, =DATEDIF(A1, TODAY(), "y") will calculate the years between the start date in A1 and the current date, updating automatically every time the spreadsheet is opened.
Troubleshooting
If you see a #VALUE! error, double-check that the start and end date cells contain valid date values. Ensure they are not formatted as text or numbers. Select the cells and try changing their format to "Short Date" or "Long Date" to see if Excel recognizes the data.
If you see a #NUM! error, verify that you are using the correct unit code for DATEDIF. The unit must be one of the following, surrounded by double quotes: "y", "m", or "d". Do not use spaces or other words.
If the result is not what you expect, consider the order of the dates. The first date argument of DATEDIF must be the start date (earlier) and the second must be the end date (later). If you swap them, you will get the negative of the expected result. You can also use the ABS() function to always get a positive result, such as =ABS(DATEDIF(A1, B1, "y")).
Frequently Asked Questions
How do I calculate the number of years between a start date and today?
Use the TODAY() function as the second argument in your DATEDIF formula. For example, =DATEDIF(A1, TODAY(), "y") will calculate the years from the date in cell A1 to the current date.
Why does the DATEDIF function not appear in the Excel formula autocomplete?
The DATEDIF function is a legacy function from Lotus 1-2-3. Microsoft included it for compatibility reasons, but they did not document it in the Excel help files. Because it is not documented, it does not appear in the autocomplete list when you type it.
Can I use DATEDIF to calculate the difference in months?
Yes, you can. To calculate the complete number of months between two dates, change the unit code in the third argument to "m". For example, =DATEDIF(A1, B1, "m").
What is the difference between "y" and "ym" in the unit argument?
The "y" unit calculates the full difference in years. The "ym" unit calculates the difference in months, ignoring the full years that have passed. For example, between 01/01/2018 and 15/06/2023, "y" returns 5, while "ym" returns 5 (the remaining months).
Does DATEDIF work with dates in the future?
Yes, DATEDIF works with dates in the future. If the end date is later than the start date, it will return a positive number. If the end date is earlier, it will return a negative number.
How do I calculate the number of years between two dates if I want to include the partial year?
The DATEDIF function with "y" only counts complete years. To include a partial year, you would need to use a different approach, such as calculating the total days and dividing by 365.25, or using the YEARFRAC function.