AX 2012 X++: Add N working days to a date
This post provides a few tips, specially focusing the working calendar and working days
Audience: Dynamics AX 2012 (FP, R2 R3) X++ Developers, Functional and Technical consultants and advanced users
Date operations are every day common tasks for any developer working on a business application. Any good programming language, platform or framework would provide well designed library(ies) with sufficient functionality abstracted in various helper classes and methods to achieve the common and extended date operations. After all, what we want to achieve is re-usability and not reinventing the wheel every now and then.
AX also has alot of such reusable method to save time and keep consistency. A few are listed below;
- DateTimeUtil: A date time manipulation utility class. Some of its methods are referenced below too
- DateTimeUtil::getSystemDateTime(): Works like a Now() method
- DateTimeUtil::addMonths(): Add n months to the provided date
- dayOfMth(date): Calculates the number of the day in the month for the specified date.
- mkDate(int month, day, year)- Creates a date based on three integers, which indicate the day, month, and year, respectively.
- prevYr(date) - Retrieves the date in the previous year that corresponds most closely to the specified date. Similar functions include prevMth() and prevQtr()
- dateMthFwd(date, int qty): Adds the specified number of months to a date.
These all methods are so useful and handy while there are much more to mention. See links section for more.
We are more interested in the working calendar. I recently got a scenario in which I had to validate the date from calendar. Then I had to decide the ending date (the famous 'To' date). The resulting date must be a valid date on a calendar. So should I write some algo for it. Experience says such a thing should be available in the library / framework somewhere. Yes this is what is to be shared with all.
WorkCalendarSched.schedDate Method
This sweet method in short takes from date, no of days to add, scheduling direction, calendar id to process against and calendar days flag to schedule the resulting date, awesome. Exactly what I was looking for. Hence using this method, we can add N working days (excluding holidays, weekEnds, etc) to any date. The resulting date is guaranteed to be a working date. Just keep your calendar tight and updated
Code sample
static void job_testSchedDate(Args _args) { WorkCalendarSched workCalendarSched; SchedDate schedDateFrom, resultantTODate; Days noOfDays; CalendarId primCalendar, secCalendar; workCalendarSched = new workCalendarSched(); noOfDays = 30; primCalendar = "Default"; secCalendar = "Default"; schedDateFrom = str2Date("21.6.2016", 123); // dd.mm.yyyy info(strfmt("From date: %1", date2str(schedDateFrom, 123, DateDay::Digits2, DateSeparator::Slash, DateMonth::Short, DateSeparator::Slash, DateYear::Digits4) )); // sequence (int) 123 means 1=dd 2=mm 3=yyyy 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) )); }
Output is shared as follows