## The Easy Way

**You’d think that there would be an AGE function that worked out how many years since the anniversary by passing in a date. **

However, life is not that easy… Fortunately there is a function called DATEDIF that does something similar. The bad news is that for some reason Microsoft no longer supports this function in Excel. You can still use it, but you don’t get any help with it. For example, you have to remember the order of the arguments that go into the function, there’s no handy pop-up box to give you a hint. So, if you’re like me, you will probably end up typing it in multiple times with the arguments in different orders until it works!

Here’s the structure of the function:

**=DATEDIF** (**Earliest Date**, **Later Date**, **Time Interval**)

The **Earliest Date** will be the date you are working with, for example the Date of Birth. The **Later Date** will be today’s date, which you can get with the TODAY function. The **Time Interval** is how you want to express the difference between the two dates. If you are working out age, then you will need to specify the interval as years by using the code “y”. If your Date of Birth is in cell A1, then the completed formula will look like this:

**=DATEDIF(A1,TODAY(),”y”)**

The DATEDIF function automatically takes into account if the person’s birthday has already occurred this year. If not, it will subtract one from the resulting age.

## The Hard Way

If you don’t want to use the unsupported DATEDIF function, then you can still work out an age from a Date of Birth, but it requires a little fancier Excel Kung Fu. There is a useful function called YEAR that will take a date and return the year bit of that date. For example, if you have the date 01/Dec/**1970** in cell A1 then =YEAR(A1) will return **1970**. (This date is very important as it’s my birthday!)

You can combine two YEAR functions to work out the difference in years between two dates:

**=YEAR(TODAY())-YEAR(A1)**

At the time of writing the date is April 2013, so the above formula would give 2013-1970, which equals 43. Sounds good so far, yes? Except, of course, I haven’t had my birthday yet, so I am actually only 42. (The extra year is increasingly important when you get to my age…)

So, to make this work, we need to somehow decide whether or not the anniversary of the date has passed. If it hasn’t, then we need to subtract one from the resulting age. This is the fancy bit!

In fact there are several ways to do this; what follows is, I think, a relatively straightforward method. We are going to work out the day of the year for each date. By this I mean: which day, out of 365, have we reached within the year in question? We can then compare the two numbers, and if the day-of-the-year number for our date of birth is less than the day-of-the-year number for today’s date, then we haven’t had our birthday yet.

Let’s break this down into parts. The first part will be to work out the day-of-the-year-number for our date of birth. We can do this by taking the date of birth and subtracting the 1^{st} of January of that year from it. When you subtract one date from another, you get the difference between those dates in days. To create the 1^{st} of January for year X we use the YEAR function, which we have already seen, and the DATE function. The DATE function creates a date from the three parts we supply: the year, month and day. If the original date is 01/Dec/1970, which is in cell A1, then the formula is:

**=A1-DATE(YEAR(A1)),1,1)**

This gives us the difference is days between the date in A1 and the 1^{st} of January 1970. (Don’t worry; it takes leap years into account!)

If we do the same for today’s date, using the TODAY function, then we get something like this:

The next step is to compare these two values using an IF function and return either 1, if the birthday hasn’t happened, or 0, if it has.

**=IF(A1-DATE(YEAR(A1),1,1)>TODAY()-DATE(YEAR(TODAY()),1,1),1,0)**

If the day-of the-year-number for the date of birth is bigger than today’s day-of-the-year number, then this means we haven’t had our birthday yet.

The final step is to put all this together into one long formula. It looks pretty scary, but if you break it down into parts, then it’s not so bad.

**=YEAR(TODAY())-YEAR(A1)-IF(A1-DATE(YEAR(A1),1,1)>TODAY()-DATE(YEAR(TODAY()),1,1),1,0)**

Don’t forget to format the result of this formula as a number; otherwise you will get some strange results.

Ian Balboa, Happy Trainer.

**If you have any Excel questions or hints and tips you would like shared get in touch, use this link!**

If you would like any further information on the Excel course we train, go to our dedicated Excel page on our website. If you are unsure of your Excel level, take our handy Excel survey.

Remember – If you’ve been on a Happy Computers course you get 2 years free helpline access. Call the helpline on 020 7375 7373.

Hi There

I have worked out my children’s age in years and months using DATEIF. I get an answer of, say, 9y 7m.

I would like to subtract their reading age, say 8y 3m, to show the difference in y and m. Is this possible?

With thanks in anticipation

Janette – we have a potential solution to your problem. Can you call the office? 020 73757300 – we’ll explain more (we have a sample file for you).

Kind regards

Hi there,

I am trying to create a formula where I have a date in a cell, and using that date along with the today formula I get the number of days between then and now…. Can you please help?!

Thanks!

Sarah – I’ve passed your query to our helpline. Can you contact the office on 020 73757300 – or email us?

We don’t have your contact information – and we’d be happy to assist you in person.

Kind regards

I did a slight modification replacing Today() for the value in B1. Making it look like this: =YEAR(B1)-YEAR(A1)-IF(A1-DATE(YEAR(A1),1,1)>B1-DATE(YEAR(B1),1,1),1,0).

Where A1 is the birth date and B1 is the date to calculate.

Since I was born in 08/06/1971, I tested for different scenarios and got these results:

– Where B1 = 8/5/2015 then the result is 43 (which is the expected result).

– Where B1 = 8/6/2015 then the result is 44 (which is the expected result).

But, where B1 = 8/5/2016, I get 45 years (the expected is 44 because 2016 is a leap year).

I think that the following approach may help with the leap year problem. The logic I used is shown below:

– First, I compared the MMDD part of the dates. If the MMDD of the date to calculate is the lesser then reutrn -1, else return 0.

– Then, add it to the difference between the YEAR part of both dates.

The resulting formula is as follows:

=IF(TEXT(B1,”MMDD”)<TEXT(A1,"MMDD"),-1,0)+YEAR(B1)-YEAR(A1)

Where A1 is the birth date and B1 is the date to calculate.

I hope this contribution helps.