## Adjust a date by month

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
BarryStains
Posts: 2
Joined: Wed May 16, 2018 1:22 am

### Adjust a date by month

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?

Barry

caseywhite
Posts: 121
Joined: Thu May 26, 2016 1:17 am

### Re: Adjust a date by month

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
Else
#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
Endif

BarryStains
Posts: 2
Joined: Wed May 16, 2018 1:22 am

### Re: Adjust a date by month

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.

Stewart Marshall
Posts: 417
Joined: Thu Nov 05, 2015 5:25 pm

### Re: Adjust a date by month

Have a look in the xDemoWebUtilities reusable part.

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

Independent IT Consultant
www.marshallfloyd.com.au

JamesDuignan
Posts: 81
Joined: Thu Nov 26, 2015 1:43 pm

### Re: Adjust a date by month

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 2242 times
Regards,
James

Jiri
Posts: 45
Joined: Thu Feb 11, 2016 11:41 pm

### Re: Adjust a date by month

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.