Solved

Difference in months

Posted on 2004-03-31
9
413 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
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
Comment Utility
datediff("m","01/01/2000","01/03/2000")
0
 

Author Comment

by:snowy1
Comment Utility
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
 
LVL 19

Expert Comment

by:Melih SARICA
Comment Utility
In Oracle

SELECT Months_Between(To_Date('01/01/2004','DD/MM/YYYY'),To_Date('01/01/2003','DD/MM/YYYY')) FROM dual
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
Comment Utility
Mine was VB6

Datediff example above
0
 

Accepted Solution

by:
rhowella earned 125 total points
Comment Utility
'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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now