Dynamics Ax workdays to days

Ever had to calculate the number of days starting from a number of workdays and even deal with holidays.
A pretty straightforward example using the DateTimeUtil class and DayOfWk method.

static void WorkDaysToDays(Args _args)
{
    int                 workDays    = 10;
    int                 days        = 0;
    date                startDate   = systemDateGet();
    CalendarId          calendarId  = CompanyInfo::find().ShippingCalendarId;
    ;
 
    if(calendarId)
    {
 
        while(workDays > 0)
        {
            if(dayofwk(startDate) != 6 && dayofwk(startdate) != 7 && WorkCalendarDate::isDateOpen(calendarId,startDate))
            {
                workDays--;
                days++;
            }
            else
            {
                days++;
            }
            startDate = DateTimeUtil::date(DateTimeUtil::addDays(DateTimeUtil::newDateTime(startDate,0),1));
        }
        info(strfmt("Workdays = %1, Days = %2",workDays,days));
    }
    else
    {
        error("No calendarId found");
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.