We help IT Professionals succeed at work.

Difference in months

snowy1 asked
Medium Priority
Last Modified: 2010-08-05
I need to write a function to perform the calculation of the number of months between 2 given dates, unfortunately I do not have use of a datediff function in the software that I am using, however, I can strip the date down to individual years, months and days. Could someone please let me know how I can accomplish this with a series of If statements.

Thanks in advance
Watch Question

Top Expert 2006

what language ?

also what do you mean by difference in months ?

e.g. date 1 : 3-Mar-2004
     date 2 : 1-Apr-2004  
   do you want the result to be 0 (not 30/31 days apart) or 1 (month did change afterall) or 2 (Mar and Apr are different months)

also confirm that this is not a homework assignment as the problem seems to be reasonably simple



No particular language and it isn't a homework assignment, I was just being lazy. The example that you are giving I would expect the result to be 1. I have got as far as:

If mm2 > mm1 Then
    Debug.Print ((yy2 - yy1) * 12) + (mm2 - mm1)
    Exit Function
ElseIf mm1 > mm2 Then
    Debug.Print (((yyyy2 - yyyy1) - 1) * 12) + ((12 - mm2) + (12 - mm1))
    Exit Function
ElseIf dd2 < dd1 Then
    Debug.Print (((yyyy2 - yyyy1) - 1) * 12) + ((12 - mm2) + (12 - mm1))
    Debug.Print ((yyyy2 - yyyy1) * 12) + (mm2 - mm1)
End If

Before this I have passed in 2 dates and stripped them to dd, mm, yyyy. Albeit this example is in VB the application I am using can only encorporate IF statements for this calculation!

In Oracle

SELECT Months_Between(To_Date('01/01/2004','DD/MM/YYYY'),To_Date('01/01/2003','DD/MM/YYYY')) FROM dual
Top Expert 2006

In C

Method 1:

months = abs( (year2 - year1) * 12 + (month2 - month1) ) + 1;

you can convert this expression to any language syntax you want ... If you are still keen on using if else constructs let me know

Method 2:

pack the dates in struct tm .... use mktime to convert to seconds ... find difference ... use gmtime to localtime to convert the difference to struct tm .... print the result

Mine was VB6

Datediff example above
Unlock this solution and get a sample of our free trial.
(No credit card required)

The tradition of old products (ex: that excel followed) is to convert to a numbering format, beginning at a certain date, then computing the difference in numbers.

But IMO it all depends on needs, such as will the formula be used forever, for a year, or just one time, such as conversion? For my (old) example, there's the historical problem, the leap year, and the different lengths of months, plus the periodic calendar adjustment.

So I think the first need is to assess what is the real need of a project. In business, it is more for a budgetary segregation by calendar month. Thus, the diff between march 31 and April Fool's is a one month separation. Even if the clock says only five minutes or less have passed.

So I suggest, until knowing otherwise, that just identifying the month field, and numbering them from 1-12 will give you a reasonable result for the short term. It can be handled by case stmt or table lookup of series of if's (which I recommend be ordered). What format is data currently?  Is someone more interested in elapsed time, who can be satisfied with average month of 30 days (integer) ? Or goal oriented on employee productivity, where if on a two week, or weekly payroll, that 28 days may be more appropriate?  There's a major difference between exercise for classroom, and needs & behaviors in the real world

Are time periods for only the year in question, or comparing this year with the last? Look for easier way out of whatever the problem is that was presented
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.