Page 1 of 1

Increment Date Field by 6 months

Posted: Thu Jul 01, 2004 9:47 pm
by Christina Lim
hallo all,

I would like to manipulate a date field.

My date value is YYYYMMDD.
I would like to get the next 6 months from the date.

For example :
Original Date : 20030303
New Date : 20030903

Original Date : 20030703
New Date : 20040103

Am I supposed to convert to internal date for the manipulation?
Or are there any built-in transform or routines that I can use to achieve these?

Would so much appreciate your advice for the solution.

thanz and Regards,
Christina

Posted: Thu Jul 01, 2004 10:23 pm
by ray.wurlod
You can add 183 days to the internal format, or use substrings to add six months to the date. The latter would need to be done in a routine.

Code: Select all

FUNCTION AddMonthsToDate(BaseDate, Months)
Equate RoutineName To "AddMonthsToDate"
Ans = @NULL
Test = Iconv(BaseDate, "DYMD")
If Status() = 0 And BaseDate Matches "8N" Then
   If Months Matches "1N0N" : @VM : "'+'1N0N"
   Then
      Year = BaseDate[1,4]
      Month = BaseDate[5,2]
      Day = BaseDate[7,2]
      Month += 6
      If Month > 12
      Then
         Month -= 12
         Year += 1
      End
      Ans = Fmt(Year,"R%4") : Fmt(Month("R%2") : Day
   End
   Else
      Call DSTransformError("Non-integer months in Arg2.", RoutineName)
   End
End
Else
   Call DSTransformError("Invalid date " : Quote(BaseDate), RoutineName)
End
RETURN(Ans)
I leave it as an exercise to add code to handle a negative second argument.

Posted: Fri Jul 02, 2004 3:16 am
by Christina Lim
Hallo Ray,

Thanz for the routine. May I know what is the second argument for?

Regards,
Christina

Posted: Fri Jul 02, 2004 6:12 am
by smoffa
I think Ray meant for the "Months" to be a variable to store the number of months to increment the current month. So, if I'm correct, you can replace the "6" with "Months".

A negative second argument is to subtract months from current month.

Also, don't you need a ":" after the @VM?

Posted: Fri Jul 02, 2004 4:32 pm
by ray.wurlod
Well spotted, I've edited the code. :oops:
Dots and colons are SO hard to see on a laptop monitor! :wink:

Posted: Wed Jul 07, 2004 3:03 am
by Christina Lim
Thanz a lot....

I managed to put it the logic just fine.

Thanz again!!