Calculate the difference between 2 dates is not an easy task in Excel.
You must take in consideration if you want to return a result based on the gap of the months or based of the years, or both.
What is the problem?
Let's suppose that you have these 2 dates
The result between this 2 dates is 9 days. But the months are not the same.
So if we use the MONTH function, the result is 1 and it's wrong, wrong, WRONG❗❗❗
DATEDIF is a hidden function 🤨🤔
The previous example showed that a difference between 2 dates is NEVER an easy calculation.
But also, how do we want to return the result?
- By year?
- By month?
- Or by month in a year?
To perform all these calculations, Excel has an incredible function to realize all the different calculation between 2 dates. It is the DATEDIF function.
Unfortunately, this function is hidden (wh ... what ???) 😲😲😲
To find out, open the help for this function. You won't see it in the list of function but it works perfectly (they're weird at Microsoft ????)
The DATEDIF function
This function requires 3 arguments:
- The first date
- The second date (necessarily greater than the first date)
- Type of calculation
The 3rd parameter of the function (the most important)
The first 3 codes are easy to understand
- d (day) calculates the difference in number of days between the 2 dates.
- m (month) calculates the difference in number of months
- y (year) calculates the difference in number of years
The other codes return a value within month or day limits
- ym will return the number of months inside a year. Possible value between 0 and 11.
- md will return a value between 0 and 30.
- yd is less useful. This code returns the number of days in a year. Possible value between 0 and 364.
Calculate the seniority
In column B, we have the hiring dates of our employees. The second date is the current date returned by the TODAY function.
For each columns, we have the following formulas
- For the years
- For the months inside a year
- For the days inside a month