Tuesday, 10 March 2020

How to get forward and backward working date from current date in AX D365?


In AX D365, there are a lot of date methods to get a particular date of day month or year. If u want to get a forward date/backward date of working days only. The following code will help you to achieve your target.

    /// <summary>

    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    { 
        WorkCalendarSched       workCalendarSched;
        SchedDate               schedDateFrom, resultantTODate;
        Days                    noOfDays;
        CalendarId              primCalendar, secCalendar;

        workCalendarSched = new workCalendarSched();

        noOfDays            = 30;
        primCalendar        = "Default";   
        secCalendar         = "Default";
        schedDateFrom       = str2Date("31.1.2019", 123);    // dd.mm.yyyy

        info(strfmt("From date: %1",
        date2str(schedDateFrom, 123, DateDay::Digits2, DateSeparator::Slash,
     DateMonth::Short, DateSeparator::Slash, DateYear::Digits4)));
        
        info(strFmt("Adding %1 days [Working days]", noOfDays));
        resultantTODate = workCalendarSched.schedDate(  SchedDirection::Forward, // Selecting backward will minus the noOfDays
                                                        schedDateFrom,
                                                        noOfDays,                   // number of days to add
                                                        NoYes::Yes,                 // Yes mean bring WorkDays        (exclude weekEnds and other configured holidays in between)
                                                        primCalendar, secCalendar); // which calendar to be used for date calculation and verification
        info(strfmt("Resultant TO date: %1",
        date2str(resultantTODate, 123, DateDay::Digits2, DateSeparator::Slash, DateMonth::Short, DateSeparator::Slash, DateYear::Digits4)));

    }

Below is the screenshot of code run of AX D365 F&O