Solved

Difference in months

Posted on 2004-03-31
9
420 Views
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
0
Comment
Question by:snowy1
[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
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10730230
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
 
LVL 2

Expert Comment

by:MrPan
ID: 10730466
datediff("m","01/01/2000","01/03/2000")
0
 

Author Comment

by:snowy1
ID: 10730471
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
Technology Partners: 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!

 
LVL 19

Expert Comment

by:Melih SARICA
ID: 10730490
In Oracle

SELECT Months_Between(To_Date('01/01/2004','DD/MM/YYYY'),To_Date('01/01/2003','DD/MM/YYYY')) FROM dual
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10730585
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
 
LVL 2

Expert Comment

by:MrPan
ID: 10731095
Mine was VB6

Datediff example above
0
 

Accepted Solution

by:
rhowella earned 125 total points
ID: 10731271
'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
 
LVL 24

Expert Comment

by:SunBow
ID: 10735633
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
 
LVL 24

Expert Comment

by:SunBow
ID: 10735659
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Starting up a Project

717 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