Solved

Excel - Number of calendar months

Posted on 2014-10-18
15
179 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 80

Assisted Solution

by:byundt
byundt earned 300 total points
Comment Utility
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 ]
Comment Utility
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 80

Expert Comment

by:byundt
Comment Utility
Here is a sample workbook showing the formula in action.
WholeMonthsBetweenTwoDatesQ-28540002.xls
0
 
LVL 80

Expert Comment

by:byundt
Comment Utility
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 ]
Comment Utility
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 80

Expert Comment

by:byundt
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 80

Expert Comment

by:byundt
Comment Utility
Second attempt to post my file, using a shorter filename:
WholeMonthsBetweenDatesQ-28540002.xlsx
0
 
LVL 80

Expert Comment

by:byundt
Comment Utility
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 ]
Comment Utility
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
Comment Utility
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 80

Expert Comment

by:byundt
Comment Utility
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
Comment Utility
Hello Brad,

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

regards, barry
0
 

Author Closing Comment

by:alisonthom
Comment Utility
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
Comment Utility
sorry... I meant to say "I would like to thank all 3 of you..."
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
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 simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

8 Experts available now in Live!

Get 1:1 Help Now