F_RELATIVE_MONTH

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Function Name : f_relative_month
// Argument Name : ad_source, Arg Type : Date, Pass By : Value
//                           al_month, Arg Type : Long, Pass By : Value
// Return Type : Date
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//    Description:   Given a date, will return the date +/- the number of months passed
//                        in the second parameter.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

integer li_adjust_months
integer li_adjust_years
integer li_month
integer li_year
integer li_day
integer li_temp_month

//Check parameters
If IsNull(ad_source) or IsNull(al_month) Then
    date ldt_null
    SetNull(ldt_null)
    Return ldt_null
End If

//Check for invalid date
If Not f_Is_Valid_Date(ad_source) Then
    Return ad_source
End If
   
//Number 12 is for the Twelve months in a year.
li_adjust_months = mod(al_month, 12)
li_adjust_years = (al_month / 12)

li_temp_month = Month(ad_source) + li_adjust_months
If li_temp_month > 12 Then
    // Add one more year and adjust for the month
    li_month = li_temp_month - 12
    li_adjust_years ++
ElseIf li_temp_month <= 0 Then
    // Subtract one more year and adjust for the month
    li_month = li_temp_month + 12
    li_adjust_years --
Else
    // No need for any adjustments
    li_month = li_temp_month
End If

li_year = Year(ad_source) + li_adjust_years
li_day = Day(ad_source)

//Check for a valid day (i.e., February 30th is never a valid date)
Do While Not f_Is_Valid_Date(Date(li_year, li_month, li_day)) &
        and li_day > 0
    li_day --
Loop

Return( Date(li_year, li_month, li_day))

0 comments:

Post a Comment