Increment a date - Forum - OpenEdge Development - Progress Community
 Forum

Increment a date

  • I need to add 2 days to a date. I know I have to adjust for a leap year and if the day happens to be greater than or equal to the 28th and for some months having 30 or 31 days. I am stuck at the beginning. I try to create the first  condition to check for the actual day. If I assign before the if loop  the first part works. But using the assign in the if/else loop fails. What am I doing wrong... if you have other ideas on the month or 30 vs 31 days I would appreciate that. Thanks.

    DO:
     
      ASSIGN FRAME fmain txtName.
     
      DEF VARIABLE v_date_increment AS CHAR.
     
     /* ASSIGN v_date_increment = STRING(DAY(TODAY),"99"). */
     
     
     IF DAY(TODAY) LE 28 THEN
     ASSIGN v_date_increment = STRING(DAY(TODAY) + 2,"99").
      MESSAGE "Hello, " v_date_increment "!" VIEW-AS ALERT-BOX.
       
      ELSE
      ASSIGN v_date_increment = STRING(DAY(TODAY),"99").
       MESSAGE "Hello, " txtName "!" VIEW-AS ALERT-BOX.

        
        
     
      txtName = "".
     
      DISPLAY txtName WITH FRAME fmain.
     
      APPLY "ENTRY" TO txtName.
     
    END.

  • Wby don't you simply do...
     
    MyDateVariable = MyDateVariable + 2.
     

  • I am doing that in STRING(DAY(TODAY) + 2... but there will be conditions where the day is not valid (ie. day is the 31st, it is february 27th...)

  • DEFINE VARIABLE MyDateVariable AS DATE NO-UNDO.
    
    MyDateVariable = TODAY + 2.

    DISPLAY "Current Date: " TODAY "Two Days From Now: " MyDateVariable.


  • hermes, you are incorrect.  Just increment the date variable itself, not the components (month, day, year).  This is one of the things that the ABL makes very, very simple.  You are thinking "old style" and making it much harder than it needs to be.

  • STRING(DAY(TODAY + 2))
     
    Steve Southwell
    Senior Consultant
    progress-bravepoint-logo2
    www.bravepoint.com
    Direct: 469-322-4265
     
    From: hermes [mailto:bounce-hermes@community.progress.com]
    Sent: Monday, August 31, 2015 9:38 AM
    To: TU.OE.Development@community.progress.com
    Subject: RE: [Technical Users - OE Development] Increment a date
     
    Reply by hermes

    I am doing that in STRING(DAY(TODAY) + 2... but there will be conditions where the day is not valid (ie. day is the 31st, it is february 27th...)

    Stop receiving emails on this subject.

    Flag this post as spam/abuse.

  • You can use the add-interval function where you even can specify the "unit" ('days', 'months', 'weeks', ...):

    MESSAGE add-interval(TODAY, 2, "days")

       VIEW-AS ALERT-BOX INFO BUTTONS OK.

  • Brian,

    Yes on it's face that is correct but I was trying another way because there is formatting that needs to be done. The date should appear as 20150831 for today and not as 08/31/2015.

  • use the add-interval and do the formatting afterwards

    assign

     vdtDate = add-interval(TODAY, 2, "days").

    assign vcFormattedDate = string(year(vdtDate), "9999":u) + string(month(vdtDate), "99":u) + string(day(vdtDate), "99":u).

  • you can always re-format your date after the calculation:

    define variable vdtDummy as date no-undo.

    vdtDummy = add-interval(TODAY, 2, "days").

    message string(year(vdtDummy), "9999") + string(month(vdtDummy), "99") + string(day(vdtDummy), "99")

    view-as alert-box info.

  • All you have to is add the number of days as suggested... which always gives you a proper date regardless of leap year or switching months.

    Then format the resulting variable by parsing the year,month and day...


    DEFINE VARIABLE myDate AS DATE NO-UNDO. ASSIGN myDate = TODAY + 2. MESSAGE STRING(YEAR(myDate),"9999") + STRING(MONTH(myDate),"99") + STRING(DAY(myDate),"99") VIEW-AS ALERT-BOX.



  • Do the formatting outside of the incrementing.  It will be much easier.
     

  • And instead of stringing date parts together, use the built-in iso-date function:

    message replace( iso-date( today ), "-", "" ).


  • Great, Thanks peggycole that works great... Thanks everyone for your input.