Cognito-forms – Using Cognito Forms Calculation to show a date in the future that ignores weekend days

cognito-forms

I need to show a date inside a form that is 3 business days from today. If today's date is Monday, March 21st, the calculated date would be Thursday, March 24th. I know that I can do that by using =DateTime.today.AddDays(3). However, if today's date is Thursday, March 24th, the calculated date would be Tuesday, March 29th, which is actually AddDays(5). How do I ignore Saturday and Sunday?

Bonus points if I can ignore holidays as well.

Best Answer

Date Calculations in Cognito Forms are extremely powerful and can handle most types of date calculations with enough effort.

The following answer assumes you have an OrderDate field that represents today's date, and are calculating a DeliveryDate that is three business days in the future.

  1. You can calculate three business days from the now using this calculation:

    Delivery Date

    =OrderDate.AddDays(if OrderDate.DayOfWeek = "Monday" or OrderDate.DayOfWeek = "Tuesday" then 3 else if OrderDate.DayOfWeek = "Sunday" then 4 else 5)

  2. For bonus points you can refine the delivery date to add an extra day for specific holidays:

    Holiday Delivery Date

    =DeliveryDate.AddDays(if OrderDate <= "7/4/2016" and DeliveryDate >= "7/4/2016" then 1 else if OrderDate <= "11/24/2016" and DeliveryDate >= "11/24/2016" then 1 else 0)

Here is a shared template demonstrating how this works: https://www.cognitoforms.com/templates/shared/CognitoSupport/SkipWeekdaysHolidays