Solved

Excel - Number of calendar months

Posted on 2014-10-18
15
183 Views
Last Modified: 2014-10-18
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
0
Comment
Question by:alisonthom
  • 7
  • 3
  • 2
  • +2
15 Comments
 
LVL 81

Assisted Solution

by:byundt
byundt earned 300 total points
ID: 40388928
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
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 40388930
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")
0
 
LVL 81

Expert Comment

by:byundt
ID: 40388956
Here is a sample workbook showing the formula in action.
WholeMonthsBetweenTwoDatesQ-28540002.xls
0
 
LVL 81

Expert Comment

by:byundt
ID: 40388969
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.
1st date      2nd date      "Correct" answer      Brad's formula      fanpages' formula
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

Brad
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 40388976
I cannot open your ".xls" workbook, Brad.

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.
0
 
LVL 81

Expert Comment

by:byundt
ID: 40388988
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.
0
 
LVL 25

Expert Comment

by:ProfessorJimJam
ID: 40388995
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 81

Expert Comment

by:byundt
ID: 40389057
Second attempt to post my file, using a shorter filename:
WholeMonthsBetweenDatesQ-28540002.xlsx
0
 
LVL 81

Expert Comment

by:byundt
ID: 40389072
Apparently, you get a maximum of 40 characters for the name of the attached file. I've filed an EE bug report.
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 40389080
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 ]
0
 
LVL 50

Accepted Solution

by:
barry houdini earned 200 total points
ID: 40389209
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
0
 
LVL 81

Expert Comment

by:byundt
ID: 40389260
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.

Brad
0
 
LVL 50

Expert Comment

by:barry houdini
ID: 40389272
Hello Brad,

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

regards, barry
0
 

Author Closing Comment

by:alisonthom
ID: 40389274
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
0
 

Author Comment

by:alisonthom
ID: 40389278
sorry... I meant to say "I would like to thank all 3 of you..."
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now