1D add to a date

SoloperSoloper Member Posts: 102
Hi all;

I want to insert a journal line into the Journal Line Table. For the posting date field, I want to calculate a date than insert this new date as posting date. The question is this; how to add just a day to a date? Please follow the following sample :)

FirstDate = myDateVariable
NewDate = myDateVariable + 1D

I want to do something like this but this code doesnt work. FirstDate, NewDate and myDateVariable is DATE.

I tried also followig example;

JournalLine."Posting Date":= "Posting Date" + 1D;

If I try to compile; It says "There is an error in a constant" and locate the cursor on the beginning of 1D.

What should I do?

Thanks.

Answers

  • ara3nara3n Member Posts: 9,256
    Try this.
    FirstDate = myDateVariable
    NewDate = myDateVariable + 1;
    
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    Hi,

    1D means DATE type constant. You can substract two dates (date-date = Integer), but you cannot add two dates. You can add integer to date instead.

    If by typing 1D you meant to use DateFormula constant then you should use CALCDATE function, as DateFormulas can only be used with CALCDATE

    To sum up - you have two options:

    1. NewDate := myDateVariable + 1 ; //not 1D

    2. NewDate := CALCDATE('1D',myDateVariable);

    Regards,
    Slawek
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • SoloperSoloper Member Posts: 102
    Thanks so much for the replies.

    By the way, I want to find the NEXT WEEK DAY.

    If the calculated days by the formula are Saturday or Sunday I want function to return MONDAY.

    Is that possible?

    Thanks again.
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    No, it is not possible with one DateFormula.

    You need to use Date virtual table, or use at least 2 date formulas and some logic, or play with Company Calendar (codeunit 7600 Calendar Management functions).

    Regards,
    Slawek
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • SoloperSoloper Member Posts: 102
    Soloper wrote:
    Thanks so much for the replies.

    By the way, I want to find the NEXT WEEK DAY.

    If the calculated days by the formula are Saturday or Sunday I want function to return MONDAY.

    Is that possible?

    Thanks again.

    Solved.


    WeekDayNumber:=DATE2DWY(PostingDate,1); //Getting the day number

    CASE WeekDayNumber OF // In case of the Week Day Number, adding the days to find next week day

    1,2,3,4,5:PostingDate:=CALCDATE('1D',Rec."Posting Date");
    6:PostingDate:=CALCDATE('3D',Rec."Posting Date");
    7:PostingDate:=CALCDATE('2D',Rec."Posting Date");

    END;
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    WeekDayNumber:=DATE2DWY(PostingDate,1); //Getting the day number

    CASE WeekDayNumber OF // In case of the Week Day Number, adding the days to find next week day

    1,2,3,4,5:PostingDate:=CALCDATE('1D',Rec."Posting Date");
    6:PostingDate:=CALCDATE('3D',Rec."Posting Date");
    7:PostingDate:=CALCDATE('2D',Rec."Posting Date");

    END;

    I think you need to rethink your solution... :mrgreen:

    Slawek
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • kinekine Member Posts: 12,562
    what about this:
    PostingDate:=CALCDATE('<1D>',Rec."Posting Date")
    while DATE2DWY(PostingDate,1) in [6,7] do
      PostingDate:=CALCDATE('<1D>',PostingDate)
    

    do not forget to use <> around the date formula, else it will not work for oher languages...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • SoloperSoloper Member Posts: 102
    @Slawek; why should I do that?

    @Kine; I want to find next weekday for all days. I mean IF the date is wednesday; I want the function return Thursday.
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    Soloper wrote:
    @Slawek; why should I do that?
    Test, and you will see :wink:. Kine's solution will work, your not :wink::mrgreen:
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • SoloperSoloper Member Posts: 102
    What kind of message is this? Tested and works fine. I just wonder if there is a short and more practical way to do that.

    Will you give a usefull info?

    As I wrote before, kine's solution is shoter and more practical than my way. But It doesnt work for me. Explanation is in previous messages.
  • ShedmanShedman Member Posts: 194
    WeekDayNumber:=DATE2DWY(PostingDate,1); //Getting the day number
    
    CASE WeekDayNumber OF // In case of the Week Day Number, adding the days to find next week day
      1,2,3,4,5:PostingDate:=CALCDATE('1D',Rec."Posting Date");
      6:PostingDate:=CALCDATE('3D',Rec."Posting Date");
      7:PostingDate:=CALCDATE('2D',Rec."Posting Date");
    END;
    
    In the DATE2DWY function, Monday Equals 1.

    So in your coding:
    - when it is friday (5), it will not return the next working day, but saturday ...
    - when is is saturday (6), it will not return the next working day (monday, + 2 days), but tuesday (+3 days)
    - when it is sunday (7), it will not return the next working day (monday, + 1 days), but tuesday (+2 days)

    Make any sense?

    Your first case should be 1,2,3,4,7, the second should be 5 and the third should be 6. But the shorter option provided in the topic should work also
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    Shedman wrote:
    Your first case should be 1,2,3,4,7, the second should be 5 and the third should be 6.
    Exactly.... :D Unless your working days include Saturday but not Monday :mrgreen:

    Slawek
    Slawek Guzek
    Dynamics NAV, MS SQL Server, Wherescape RED;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • SoloperSoloper Member Posts: 102
    Thanks ara3n, Slawek, Kine and Shedman.
Sign In or Register to comment.