Skip to main content
June 25, 2021
Question

Add 'n' number of days(excluding weekends and holidays) to a given date.

  • June 25, 2021
  • 3 replies
  • 0 views

Hi Community,

How to add 'n' number of days(excluding weekends and holidays) to a given date? We have defined holidays in Database. I know it is possible to define a Process Calendar with a list of non-working days and then use it in caladddays() function to achieve this outcome. As process calendars are not exportable and needs to be created in each environment separately, we are not considering this option at the moment. Having said that is there any alternative way to achieve this result?

Thanks,

Arun

3 replies

June 25, 2021

Alright, I found workday() function will help in this regard.

mikes0011
Brainy
June 25, 2021

That should help you - my additional thought was, if you're storing your holidays in your database (as opposed to going through the fairly cumbersome and not-very-flexible process of defining them manually within your Appian environment), a brute-force approach would probably be feasible too - i.e. loop over a date range (like with a!forEach) and check each date for its "day of week" as well as whether it's in your holidays DB.

The Expression Guru
csteward
June 25, 2021

Yea, I have this setup as a recursive function in some areas where additional processing is required.  Generally, fn!workday() should cover it, but if we need to expand the logic, a starting example is:

rule!chris_test_add_days():

a!localVariables(
  local!dateStart: today(), /* replace with rule input if you do not always want to add from today */
  local!addDays: 20,
  local!holidays: a!queryEntity(
    entity: cons!CHRIS_TEST_DS_HOLIDAYS,
    fetchTotalCount: false,
    query: a!query(
      /* make sure you include enough range - get the same # of future holidays as days adding */
      pagingInfo: a!pagingInfo(1,local!addDays,a!sortInfo(field: "date",ascending: true)),
      filter: a!queryFilter(
        field: "date",
        operator: ">",
        value: local!dateStart
      )
    )
  ).data,
  
  rule!chris_test_add_days_recur(
    date: local!dateStart,
    addDays: local!addDays,
    daysAdded: 0,
    holidays: property(local!holidays,"date",{})
  )
)

rule!chris_test_add_days_recur(date (Date), addDays (Integer), daysAdded (Integer), holidays (List of Date)):

if(
  ri!daysAdded=ri!addDays,
  ri!date,
  a!localVariables(
    local!nextDay: todate(caladddays(ri!date,1)),
    rule!chris_test_add_days_recur(
      date: local!nextDay,
      addDays: ri!addDays,
      daysAdded: ri!daysAdded+if(
        contains(
          ri!holidays,
          local!nextDay
        ),
        0,
        1
      )
    )
  )
)

As always with recursive functions, make sure you save your edits prior to testing!