Cognito-forms – How to calculate specific date based on the day of the week

cognito-forms

I have a form that is due on the first Tuesday of each month.
I would like to find a way to automatically populate the due date based on the current date, by showing the date of the next upcoming first Tuesday.

Any ideas?

Best Answer

Here is the specific calculation to do this in Cognito Forms:

=DateTime.Today.AddDays(1 - DateTime.Today.Day).AddMonths(1).AddDays("TueMonSunSatFriThuWed".IndexOf((DateTime.Today.AddDays(1 - DateTime.Today.Day).AddMonths(1).DayOfWeek + "").Substring(0, 3))/3)

This calculation leverages DateTime.DayOfWeek to determine the day of the week, converts this to an offset by looking up the day of the week in this text value TueMonSunSatFriThuWed, and adds the corresponding days to the first day of the month.