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.
Post Reply
BarryStains
Posts: 2
Joined: Wed May 16, 2018 1:22 am

Adjust a date by month

Post by BarryStains » Fri Aug 24, 2018 4:00 am

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

Post by caseywhite » Fri Aug 24, 2018 5:11 am

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

Post by BarryStains » Sat Sep 08, 2018 7:17 am

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.

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

Re: Adjust a date by month

Post by Stewart Marshall » Mon Sep 10, 2018 3:13 pm

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

Post by JamesDuignan » Tue Sep 11, 2018 8:49 am

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
Capture.PNG (9.63 KiB) Viewed 2261 times
Regards,
James

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

Re: Adjust a date by month

Post by Jiri » Fri Nov 01, 2019 3:22 am

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.

Post Reply