Page 1 of 1

Adjust a date by month

Posted: Fri Aug 24, 2018 4:00 am
by BarryStains
Does anyone have a way of adjusting a date by more than just days. For example, if I use the adjust function on date of say 08/30/2018 and say 60 days (because I really want 2 months), I get 10/29/2018 not 10/30/2018 (which is what the end user would expect to see). There doesn't seem to be an easy way of adjusting a date by number of months.

Has anyone else ran into this issue?


Re: Adjust a date by month

Posted: Fri Aug 24, 2018 5:11 am
by caseywhite
What about something like this.

Code: Select all

* STD_NUM is number of months to add.  Must be positive
#STD_NUM := 7
If Cond(*YYYYMMDD.AsDate( CCYYMMDD ).Month + #STD_NUM > 12)
#YYYYMMDD := ((*YYYYMMDD.AsDate( CCYYMMDD ).Year + 1).AsString + (*YYYYMMDD.AsDate( CCYYMMDD ).Month + #STD_NUM - 12).AsString.RightAdjust( 2 '0' ) + *YYYYMMDD.AsDate( CCYYMMDD ).Day.AsString.RightAdjust( 2 '0' )).AsNumber
#YYYYMMDD := (*YYYYMMDD.AsDate( CCYYMMDD ).Year.AsString + (*YYYYMMDD.AsDate( CCYYMMDD ).Month + #STD_NUM).AsString.RightAdjust( 2 '0' ) + *YYYYMMDD.AsDate( CCYYMMDD ).Day.AsString.RightAdjust( 2 '0' )).AsNumber

Re: Adjust a date by month

Posted: Sat Sep 08, 2018 7:17 am
by BarryStains
That's all fine and dandy until you encounter Dec 31st. That would make it Feb 31st which is invalid. Then you have to worry about leap year and other months that only have 30 days in them. That logic is something usually built into most programming languages date functions. I could build a complete function to do the calculations, I was just hoping there was a built in way.

Re: Adjust a date by month

Posted: Mon Sep 10, 2018 3:13 pm
by Stewart Marshall
Have a look in the xDemoWebUtilities reusable part.

I'm sure(ish) I added routine for this.

Re: Adjust a date by month

Posted: Tue Sep 11, 2018 8:49 am
by JamesDuignan
Stewart is correct, xdemoutilities has a very good example of adjusting the date and validating the adjusted date. I have used this code as a starting point for my own date validations in several programs.

If you want to see it in action run form xDemoDateHandling. This demonstrates xdemoutilities date adjusting in a very simple context.
Capture.PNG (9.63 KiB) Viewed 2255 times

Re: Adjust a date by month

Posted: Fri Nov 01, 2019 3:22 am
by Jiri
Those are good and fully working examples, but we would prefer an enhancement that LANSA would support standard date operations like many other languages. The syntax may be LANSA specific, but it should be possible to use months /years in adjustment directly, not via mathematic operation followed by adjustment checks.