# Excel - Number of calendar months

Hi

I would really appreciate help with how to calculate the number of whole calendar months between 2 dates.

For example, if the start and end dates are 1 May 2012 - 31 March 2013, I would expect the result to be 11.
But if the start date is 2 May 2012 instead of 1 May 2012, the result should be 10.

It would be very good if I could also deal with leap years.  So if the end date was 28 February 2012, the month of February would not be included in the result.  But it would be if the end date was 29 February 2012.

Thank you very much in advance
Alison
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mechanical EngineerCommented:
You might consider a formula using the undocumented function DATEDIF. This function has been part of Excel since Excel 5, but was only documented by Microsoft in Excel 2000. It can return the number of whole months between two dates as DATEDIF(FirstDate, LastDate, "M"). This value must then be adjusted (in your case) for the first date not being the start of the month, the second date not being the end of the month, and the first day (of the month) being more than the second day (of the month). These corrections are shown as Boolean expressions in the following formula.
=DATEDIF(A1,B1,"m")-(DAY(A1)>1)-(DAY(B1+1)>1)+(DAY(A1)>DAY(B1))+1

Boolean expressions like (DAY(A1)>1) return TRUE if the day of the month is larger than 1, and FALSE otherwise. If you use Boolean expressions in an arithmetic expression (like the suggested formula), the TRUE values are converted to 1 and the FALSE to 0.

For more on DATEDIF, see Chip Pearson's webpage at http://www.cpearson.com/excel/datedif.aspx
Commented:
Hi,

I think I can reach the results you require, as follows:

Cell [A1]: =01/05/2012
Cell [B1]: =31/03/2013

In any other cell: =DATEDIF(A1,B1+1,"m")
Mechanical EngineerCommented:
Here is a sample workbook showing the formula in action.
WholeMonthsBetweenTwoDatesQ-28540002.xls
Mechanical EngineerCommented:
fanpages,
Our formulas agree in some cases, but differ in others. I've bolded the test cases with discrepancies below. The column marked "Correct" answer is my admittedly biased interpretation of the problem. Note that all dates shown use MDY format.
5/1/2012      3/31/2013      11      11      11
5/2/2012      3/31/2013      10      10      10
5/1/2011      2/28/2012      9      9      9
5/1/2011      2/29/2012      10      10      10
5/2/2011      2/28/2012      8      8      9
5/2/2011      2/29/2012      9      9      9
5/15/2011      2/14/2012      8      8      9
5/15/2011      2/15/2012      8      8      9
5/15/2011      2/16/2012      8      8      9

5/1/2012      5/31/2012      1      1      1
5/1/2012      5/20/2012      0      0      0
5/1/2012      5/1/2013      12      12      12

Commented:

It does not seem to be in a format recogni[s|z]ed by MS-Excel 2003 or 2013.

PS. It is difficult to deduce the "correct" answer in every case you have listed, given just two sets of test data from Alison.
Mechanical EngineerCommented:
The workbook I posted is in .xlsx format.

I'm assuming there is a bug in the file posting code that trimmed the last x off the file name.
Microsoft Excel ExpertCommented:
DatedIF has known issues, thats why it has never been documented by microsoft.

for example check between dates 2/1/2014 to 3/1/2014   it gives wrong result.
Mechanical EngineerCommented:
Second attempt to post my file, using a shorter filename:
WholeMonthsBetweenDatesQ-28540002.xlsx
Mechanical EngineerCommented:
Apparently, you get a maximum of 40 characters for the name of the attached file. I've filed an EE bug report.
Commented:
DatedIF has known issues, thats why it has never been documented by microsoft.

DATEDIF is documented for the 'Mac' version of MS-Excel:

[ http://office.microsoft.com/en-gb/mac-excel-help/results.aspx?qu=DATEDIF&avg=zxl ]
Commented:
Hello Alison,

As far as I know the "known issues" mentioned with DATEDIF are all concerned with the use of "md", "ym" or "yd" as the 3rd parameter, I don't know of any problems with using "m" as per the suggestions here.

Brad's suggested formula will return -1 in some cases where A1 and B1 are in the same month, e.g. 2-Oct-2014 to 31-Oct-2014, so if that's a possible date range an added MAX function will give zero, i.e.

=MAX(DATEDIF(A1,B1,"m")-(DAY(A1)>1)-(DAY(B1+1)>1)+(DAY(A1)>DAY(B1))+1,0)

or for identical results, assuming A1 <= B1 you can use this formula:

=MAX(DATEDIF(A1-DAY(A1-1),B1+1,"m")-1,0)

regards, barry

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Mechanical EngineerCommented:
barry,
My formula works with 2-Oct-2014 to 31-Oct-2014, but fails with 2-Oct-2014 to 28-Oct-2014.

Your more elegant formula works in all instances, of course.

Commented:

Yes, sorry, I picked an example which didn't demonstrate my argument, D'oh!

regards, barry
Author Commented:
I would like all 3 of you so much for your input and help! I would also like to thank byundt  for checking the results against different dates and Barry for the MAX modification.

Many thanks
Alison
Author Commented:
sorry... I meant to say "I would like to thank all 3 of you..."
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.