Solved

Excel - Number of calendar months

Posted on 2014-10-18
15
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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 26

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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

734 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