• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 424
  • Last Modified:

Difference in months

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
0
snowy1
Asked:
snowy1
  • 2
  • 2
  • 2
  • +3
1 Solution
 
sunnycoderCommented:
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
0
 
MrPanCommented:
datediff("m","01/01/2000","01/03/2000")
0
 
snowy1Author Commented:
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))
Else
    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!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Melih SARICACommented:
In Oracle

SELECT Months_Between(To_Date('01/01/2004','DD/MM/YYYY'),To_Date('01/01/2003','DD/MM/YYYY')) FROM dual
0
 
sunnycoderCommented:
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
0
 
MrPanCommented:
Mine was VB6

Datediff example above
0
 
rhowellaCommented:
'declare variable

dim yDiff as integer  'difference of the two years in months
dim mdiff as integer 'total difference in months

if (yyyy1>yyyy2) then
      ydiff = (yyyy1-yyyy2)*12
      if (mm1>mm2) then
            mdiff = ydiff + (mm1-mm2)
      else
            mdiff = ydiff + (mm2-mm1)
      end if
elseif
      ydiff = (yyyy2-yyyy1)*12
      if (mm1>mm2) then
            mdiff = ydiff + (mm1-mm2)
      else
            mdiff = ydiff + (mm2-mm1)
      end if
end if

debug.print (mdiff)

'since your output requires the difference in months, you don't have to compare 'dd' unless you'll consider having fractional/decimal answers
0
 
SunBowCommented:
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
0
 
SunBowCommented:
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
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now