Calculating Age and Service Duration Using DATEDIF in Excel
Course: Advanced Excel
Topic: Calculating Age and Service Duration Using DATEDIF
Course Link: Advanced Excel Course
Demo Link: Register for a Free Course Demo
Website: Visit JustAcademy
1. Introduction to the DATEDIF Function
Microsoft Excel provides several date and time functions for calculating differences between dates. The DATEDIF function is particularly useful when calculating a person's age, employee service duration, project experience, membership periods, and other date-based intervals.
DATEDIF calculates the difference between two dates in years, months, or days. Depending on the selected unit, it can return complete years, complete months, total days, or the remaining months and days after complete years.
For example, an HR department can use DATEDIF to calculate how long an employee has worked for an organization. Similarly, a school administrator can calculate a student's age from their date of birth.
DATEDIF is a legacy Excel function that remains available in supported versions. Excel may not display it in the usual formula autocomplete list, so users often need to enter the formula manually.
2. Learning Objectives
- Understand the purpose and syntax of DATEDIF.
- Calculate age in complete years.
- Calculate age in years, months, and days.
- Determine employee service duration.
- Calculate the total number of months or days between two dates.
- Use DATEDIF with TODAY for automatically updating calculations.
- Handle common errors and date-related issues.
- Apply DATEDIF to HR, education, membership, and business datasets.
3. Understanding the DATEDIF Function
The DATEDIF function calculates the difference between a starting date and an ending date according to a specified time unit.
Syntax
=DATEDIF(start_date,end_date,unit)
Function Arguments
| Argument |
Description |
Example |
| start_date |
The earlier date from which the calculation begins. |
A2 containing a date of birth |
| end_date |
The later date up to which the difference is calculated. |
TODAY() |
| unit |
A text code specifying the type of difference to calculate. |
"Y" |
Important: The start date must be earlier than or equal to the end date. If the start date is later than the end date, DATEDIF returns a #NUM! error.
4. Understanding DATEDIF Units
The unit argument determines what DATEDIF returns. The following units are commonly used:
| Unit |
Meaning |
Example Use |
| "Y" |
Complete years between two dates. |
Age in years |
| "M" |
Complete months between two dates. |
Total complete months of service |
| "D" |
Total days between two dates. |
Total days since joining |
| "YM" |
Remaining months after subtracting complete years. |
Remaining months in an age or service duration |
| "YD" |
Days between dates, ignoring the difference in years. |
Days within the year-based portion of a date comparison |
| "MD" |
Remaining days after subtracting complete months and years. |
Remaining days in a years-months-days breakdown |
For most age and service calculations, the "Y", "YM", and "MD" units are combined to display the duration in years, months, and days.
5. Calculating Age in Complete Years
One of the most common uses of DATEDIF is calculating a person's age from their date of birth.
Example Dataset
| Person |
Date of Birth |
Age Formula |
| Rahul |
15-Mar-1995 |
=DATEDIF(B2,TODAY(),"Y") |
| Priya |
22-Jul-1998 |
=DATEDIF(B3,TODAY(),"Y") |
| Amit |
10-Dec-2000 |
=DATEDIF(B4,TODAY(),"Y") |
Formula
=DATEDIF(B2,TODAY(),"Y")
Explanation:
- B2 contains the person's date of birth.
- TODAY() returns the current date.
- "Y" instructs Excel to calculate complete years.
The formula returns the person's age in completed years. It does not increase the age until the person's birthday has occurred in the current year.
Example with Fixed Dates
Suppose a person was born on 15-Mar-2000 and the calculation date is 27-Oct-2026.
=DATEDIF(DATE(2000,3,15),DATE(2026,10,27),"Y")
Result: 26 years.
Using fixed dates in examples makes results reproducible. Using TODAY() makes the result update automatically as time passes.
6. Calculating Age in Years, Months, and Days
Sometimes age in completed years is not detailed enough. For official records, eligibility checks, or detailed reports, you may need to display age in years, months, and days.
Formula
=DATEDIF(B2,TODAY(),"Y")&" Years, "&DATEDIF(B2,TODAY(),"YM")&" Months, "&DATEDIF(B2,TODAY(),"MD")&" Days"
Explanation:
DATEDIF(B2,TODAY(),"Y") calculates complete years.
DATEDIF(B2,TODAY(),"YM") calculates remaining complete months after complete years are removed.
DATEDIF(B2,TODAY(),"MD") calculates remaining days after complete months and years are removed.
- The ampersand (&) joins the calculated values with descriptive text.
Example
Suppose the date of birth is 15-Mar-2000 and the calculation date is 27-Oct-2026.
=DATEDIF(DATE(2000,3,15),DATE(2026,10,27),"Y")&" Years, "&DATEDIF(DATE(2000,3,15),DATE(2026,10,27),"YM")&" Months, "&DATEDIF(DATE(2000,3,15),DATE(2026,10,27),"MD")&" Days"
Result: 26 Years, 7 Months, 12 Days.
This format is useful when an organization needs an exact age breakdown rather than a simple whole-number age.
7. Calculating Employee Service Duration
Employee service duration is the period between an employee's joining date and a specified ending date. HR teams use this calculation to maintain employee records, analyze workforce experience, and support service-based reporting.
Example Dataset
| Employee |
Joining Date |
Service Duration Formula |
| Rahul |
10-Jan-2018 |
=DATEDIF(B2,TODAY(),"Y") |
| Priya |
15-Jun-2020 |
=DATEDIF(B3,TODAY(),"Y") |
| Amit |
01-Sep-2022 |
=DATEDIF(B4,TODAY(),"Y") |
Calculate Service in Complete Years
=DATEDIF(B2,TODAY(),"Y")
This formula calculates the employee's complete years of service from the joining date to the current date.
Calculate Service in Years and Months
=DATEDIF(B2,TODAY(),"Y")&" Years, "&DATEDIF(B2,TODAY(),"YM")&" Months"
This formula displays the employee's complete years of service along with the remaining complete months.
Calculate Service in Years, Months, and Days
=DATEDIF(B2,TODAY(),"Y")&" Years, "&DATEDIF(B2,TODAY(),"YM")&" Months, "&DATEDIF(B2,TODAY(),"MD")&" Days"
This formula produces a more detailed service-duration result for employee reports.
8. Calculating Service Duration Up to a Specific Date
In some situations, service duration must be calculated up to a specific date rather than today. Examples include a past appraisal date, the end of a financial year, or an employee's last working day.
Suppose B2 contains the joining date and C2 contains the ending date.
Formula for Complete Years of Service
=DATEDIF(B2,C2,"Y")
Formula for Complete Months of Service
=DATEDIF(B2,C2,"M")
Formula for Total Days of Service
=DATEDIF(B2,C2,"D")
Example: If the joining date is 10-Jan-2020 and the ending date is 10-Jan-2025, the formulas return:
- Complete years: 5
- Complete months: 60
- Total days: 1,827
The total number of days depends on the actual calendar dates, including leap years. Excel calculates the difference between the two dates rather than counting both boundary dates as separate days.
9. Calculating the Remaining Months and Days
The "YM" and "MD" units are especially useful when you need to break a duration into its components.
Example: Remaining Months
=DATEDIF(B2,C2,"YM")
This returns the remaining complete months after complete years have been excluded.
Example: Remaining Days
=DATEDIF(B2,C2,"MD")
This returns the remaining days after complete months and years have been excluded.
For example, a service period of 5 years, 3 months, and 12 days can be represented using separate DATEDIF formulas for "Y", "YM", and "MD".
Note: The "MD" unit has known limitations in certain month-end and leap-year scenarios. For critical financial, legal, or contractual calculations, verify the result against the applicable rules or use a date-difference method that explicitly handles calendar boundaries.
10. Using DATEDIF with TODAY and IF
DATEDIF can be combined with TODAY and IF to make date calculations dynamic and handle missing data more safely.
Example 1: Automatically Updating Age
=DATEDIF(B2,TODAY(),"Y")
The result updates when the workbook recalculates on a later date. This is useful for live employee or customer records.
Example 2: Calculate Age Only When a Birth Date Exists
=IF(B2="","",DATEDIF(B2,TODAY(),"Y"))
If B2 is blank, the formula returns a blank result instead of displaying an age calculation.
Example 3: Calculate Service Duration Only for Valid Joining Dates
=IF(B2="","",IF(B2>TODAY(),"Invalid Joining Date",DATEDIF(B2,TODAY(),"Y")))
This formula checks whether the joining-date cell is blank or contains a future date. It returns a message for a future joining date and calculates complete years for a valid past date.
For records with an ending date, also check that the ending date is not earlier than the joining date before calculating the difference.
11. Common Errors and Troubleshooting
| Error or Problem |
Possible Cause |
Solution |
| #NUM! |
The start date is later than the end date. |
Check that the earlier date is the first argument. |
| #VALUE! |
A date argument contains invalid text or an unsupported value. |
Convert the entries into valid Excel dates. |
| Incorrect age |
The formula uses a simple year subtraction instead of complete-year calculation. |
Use DATEDIF with the "Y" unit. |
| Unexpected months or days |
The selected unit does not represent the required duration. |
Use "Y", "M", "D", "YM", "YD", or "MD" appropriately. |
| Formula not suggested by Excel |
DATEDIF is a legacy compatibility function. |
Enter the formula manually and verify the spelling. |
| Age is blank or incorrect |
The date-of-birth cell is empty or stored as text. |
Check the cell value and date format before calculating. |
| Unexpected result around month-end |
Calendar-month lengths and leap years affect date differences. |
Test the calculation with the actual dates and verify business rules. |
12. Best Practices for Using DATEDIF
- Use valid Excel date values for both the start and end dates.
- Use "Y" to calculate complete years rather than subtracting the birth year from the current year.
- Use "M" when you need total complete months, not just the remaining months.
- Combine "Y", "YM", and "MD" when a years-months-days breakdown is required.
- Use TODAY() for calculations that should update automatically.
- Use a fixed end-date cell for historical reporting or reproducible calculations.
- Validate that the end date is not earlier than the start date.
- Use IF to handle blank cells and invalid date ranges.
- Be careful with the "MD" unit for month-end and leap-year calculations.
- Check the organization's policies before using service duration for benefits, promotions, leave eligibility, or payroll decisions.
13. Practice Exercises
Exercise 1: Calculate Age
Enter the date of birth 20-May-2001 in cell A2.
- Calculate the person's age in complete years using TODAY().
- Calculate the age in years, months, and days.
- Replace TODAY() with a fixed date to test the formula.
Exercise 2: Calculate Employee Service
An employee joined the company on 12-Aug-2019.
- Calculate complete years of service.
- Calculate total complete months of service.
- Display the duration in years, months, and days.
Exercise 3: Calculate Service to a Past Date
An employee joined on 05-Feb-2020 and left on 30-Nov-2025.
- Calculate the complete years of service.
- Calculate the total complete months.
- Calculate the total number of days between the dates.
Exercise 4: Handle Blank Dates
Create a list of employees where some date-of-birth cells are blank.
- Use IF to avoid displaying an age when the date is missing.
- Check how the formula behaves when a date is stored as text.
- Correct invalid records before calculating ages.
Exercise 5: Validate Joining Dates
Create a service-duration formula that returns a message if the joining date is in the future or is later than the specified end date.
- Use IF to validate the date range.
- Use DATEDIF for valid date pairs.
- Test the formula with blank, valid, and invalid dates.
14. Mini Project: Employee Age and Service Duration Report
Project Objective: Create an Excel report that calculates employee age and service duration automatically.
Suggested Dataset
| Employee ID |
Employee Name |
Date of Birth |
Joining Date |
Age |
Service Duration |
| EMP101 |
Rahul Sharma |
15-Mar-1995 |
10-Jan-2018 |
Calculated using DATEDIF |
Calculated using DATEDIF |
| EMP102 |
Priya Shah |
22-Jul-1998 |
15-Jun-2020 |
Calculated using DATEDIF |
Calculated using DATEDIF |
| EMP103 |
Amit Verma |
10-Dec-2000 |
01-Sep-2022 |
Calculated using DATEDIF |
Calculated using DATEDIF |
| EMP104 |
Neha Patil |
05-Feb-1992 |
18-Apr-2016 |
Calculated using DATEDIF |
Calculated using DATEDIF |
Project Steps
- Enter employee IDs, names, dates of birth, and joining dates.
- Use DATEDIF with the "Y" unit to calculate age.
- Use DATEDIF with the "Y", "YM", and "MD" units to calculate service duration.
- Use IF to handle blank dates and invalid date ranges.
- Copy formulas down the employee table.
- Format date columns consistently.
- Check the results using a fixed reference date when preparing historical reports.
- Create a summary showing employee ages and years of service.
Expected Outcome: A reusable HR worksheet that calculates employee age and service duration, reduces manual calculations, and supports workforce reporting.
15. Interview Questions and Answers
Q1. What is the DATEDIF function in Excel?
Answer: DATEDIF calculates the difference between two dates in complete years, months, days, or selected combinations of these units.
Q2. How do you calculate age in complete years?
Answer: Use =DATEDIF(A2,TODAY(),"Y"), assuming A2 contains the date of birth.
Q3. How do you calculate employee service duration?
Answer: Use DATEDIF with the employee's joining date as the start date and the current date or last working date as the end date.
Q4. What is the difference between "Y" and "M"?
Answer: "Y" returns complete years, while "M" returns total complete months between the two dates.
Q5. What does the "YM" unit return?
Answer: It returns the remaining complete months after complete years are excluded.
Q6. What is the purpose of TODAY()?
Answer: TODAY() returns the current date and allows age or service-duration calculations to update automatically when Excel recalculates.
Q7. Why might DATEDIF return #NUM!?
Answer: A common cause is that the start date is later than the end date.
Q8. Can DATEDIF calculate the total number of days?
Answer: Yes. Use the "D" unit to calculate the number of days between the two dates.
Q9. Is DATEDIF available through Excel's autocomplete suggestions?
Answer: It may not appear in formula autocomplete because it is a legacy compatibility function, but it remains available in supported Excel versions.
Q10. What should you consider when using DATEDIF for HR reports?
Answer: Confirm the reference date, validate date entries, handle blank cells, and follow company policies for service and benefit calculations.
16. Quick Revision Checklist
- DATEDIF calculates the difference between two dates.
- "Y" returns complete years.
- "M" returns total complete months.
- "D" returns total days between the dates.
- "YM" returns remaining complete months after full years.
- "YD" calculates the day difference while ignoring the year difference.
- "MD" returns remaining days after complete months and years are excluded.
- TODAY() can make age and service calculations dynamic.
- IF can handle blank cells and invalid date ranges.
- Always validate dates and test month-end and leap-year cases.
17. Conclusion
The DATEDIF function is a useful Excel tool for calculating age, employee service duration, and the time between two dates. By selecting the appropriate unit, users can calculate complete years, total months, total days, and detailed years-months-days breakdowns.
Combining DATEDIF with TODAY and IF makes date-based reports more flexible and reliable. These techniques are valuable for HR teams, administrators, data analysts, and advanced Excel users who work with employee records, customer data, and time-based reporting.
Explore more practical Excel skills through the JustAcademy Advanced Excel Course or register for a course demo.