Difference in years/months between two dates

Archive of postings to DataStageUsers@Oliver.com. This forum intended only as a reference and cannot be posted to.

Moderators: chulett, rschirm

Locked
admin
Posts: 8720
Joined: Sun Jan 12, 2003 11:26 pm

Difference in years/months between two dates

Post by admin »

This has been published on www.dsxchange.com in the past. The actual solution depends on whether you want exact months (for example 5.5), or completed months (for example 5).
Subtracting the internal formats gives you the interval in days, which is not precise.
Respond whether you want completed months or exact months and I'll post a routine that does it for you.
_______________________________________________
datastage-users mailing list
datastage-users@oliver.com
http://www.oliver.com/mailman/listinfo/datastage-users
<b>PLEASE READ</b>
Do not contact admin unless you have technical support or account questions. Do not send email or Private Messages about discussion topics to ADMIN. Contact the webmaster concerning abusive or offensive posts.
admin
Posts: 8720
Joined: Sun Jan 12, 2003 11:26 pm

Difference in years/months between two dates

Post by admin »

The function below returns completed months.
You can get years by diving by 12 and taking the integer portion.
Months by dividing by 12 and obtaining the remainder.

DEFFUN CompletedMonths(Arg1,Arg2) CALLING "DSU.CompletedMonths"
TotalMonths = CompletedMonths(startDate, finishDate)
Years = Int(TotalMonths/12)
Months = Mod(TotalMonths, 12)


=======================================================================
FUNCTION CompletedMonths(Date1,Date2)
$COPYRIGHT "Copyright (c) 2003, Ray Wurlod. All rights reserved."
*
* Purpose
* Returns the number of completed months between Date1 and Date2.
*
* History
* Date..... Version Programmer.... Details of Modification
* 26 Jul 03 2.0.0 Ray Wurlod Initial coding
*

Equate RoutineName To "CompletedMonths"

* Modify the following constant for other languages.
Equate ErrorMessage1 To "Input argument is not a valid internal format date for this routine."

* Date1 and Date2 must be valid internal format dates. In this routine
* a valid internal format date is an integer with five or fewer digits.
* This means that dates earlier than 17 Mar 1694 or later than 14 Oct 2241
* will be rejected. It is not felt that this is a major limitation.
* To bypass testing for this, undefine the following token.
$DEFINE TestDates

* The following token defines whether a null result will be reported as
* a "true" null or as the string "" for display in the Test grid.
$UNDEFINE TestNull

* Initialize result assuming that there will be an error of some kind.
Ans = @NULL

$IFDEF TestDates
TestDate1 = Oconv(Date1,"R-99999,99999")
TestDate2 = Oconv(Date2,"R-99999,99999")
If Not(Len(TestDate1)) And Not(Len(TestDate2))
Then
Call DSTransformError(ErrorMessage1, RoutineName)
GoTo MainExit
End
$ENDIF

* If Date1 > Date2 then the result is zero.
If Date1 > Date2
Then
Ans = 0
GoTo MainExit
End

* Generate external formats and decompose.
ExtDate1 = Oconv(Date1, "D-YMD[4,2,2]")
Year1 = ExtDate1[1,4]
Month1 = ExtDate1[6,2]
Day1 = ExtDate1[9,2]
ExtDate2 = Oconv(Date2, "D-YMD[4,2,2]")
Year2 = ExtDate2[1,4]
Month2 = ExtDate2[6,2]
Day2 = ExtDate2[9,2]

* Calculated completed months based on decomposed dates
YearDiff = Year2 - Year1
MonthDiff = Month2 - Month1
If MonthDiff
<b>PLEASE READ</b>
Do not contact admin unless you have technical support or account questions. Do not send email or Private Messages about discussion topics to ADMIN. Contact the webmaster concerning abusive or offensive posts.
Locked