Solved

Excel - Number of calendar months

Posted on 2014-10-18
15
187 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA Help 18 45
Unhide very hidden sheets with password 22 45
Excel formula to display cell reference of named range 5 39
excel formula to sum column 13 15
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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 Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

821 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