How-to: Creating Conditions for Date Form Fields

Conditions based on the selected date of a date form field can be defined by a simple date comparison or in a more complex way, where recurring events compare the date with a range of days that are related to a specific year, month, week or weekday.

Date comparison

A date comparison condition compares the value of a date form field with a specific date. Besides using the basic comparison operators Is and Is not, the date form field value can be tested, if it is before or after the specified date.

Some examples for date comparisons are listed as follows:

Date form field is empty

There is no guarantee that a date field will always indicate a specific date. To test whether a date is not set, the Is operator is used in combination with the corresponding Empty (no value set)flag.

Image

To create this condition programmatically, use the following code snippet:

// Create conditions
Condition cdIsEmpty = new Condition(m_dffDateFormField, Condition.ComparisonOperators.Is, null);
Condition[] rcdConditions = new Condition[] { cdIsEmpty };
' Create conditions
Dim cdIsEmpty As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[Is], Nothing)
Dim rcdConditions As Condition() = New Condition() { cdIsEmpty }

03/11/2020

The possibly most simple date field condition is to check whether the date form field represents a specific date. In case the Is operator is used in combination with the specified date.

Image

To create this condition programmatically, use the following code snippet:

// Create conditions
Condition cdSpecificDate = new Condition(m_dffDateFormField, Condition.ComparisonOperators.Is, new DateTime(2020, 03, 11));
Condition[] rcdConditions = new Condition[] { cdSpecificDate };
' Create conditions
Dim cdSpecificDate As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[Is], New DateTime(2020, 03, 11))
Dim rcdConditions As Condition() = New Condition() { cdSpecificDate }

Except 03/11/2020

If all other dates, except a specified date, should fulfill the condition, the Is not operator has to be used.

Image

To create this condition programmatically, use the following code snippet:

// Create conditions
Condition cdExceptSpecificDate = new Condition(m_dffDateFormField, Condition.ComparisonOperators.IsNot, new DateTime(2020, 03, 11));
Condition[] rcdConditions = new Condition[] { cdExceptSpecificDate };
' Create conditions
Dim cdExceptSpecificDate As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[IsNot], New DateTime(2020, 03, 11))
Dim rcdConditions As Condition() = New Condition() { cdExceptSpecificDate }

Before 03/11/2020

To define a condition looking for a date before the specified comparison date value, the Before operator is used.

Image

To create this condition programmatically, use the following code snippet:

// Create conditions
Condition cdBeforeSpecificDate = new Condition(m_dffDateFormField,
Condition.ComparisonOperators.IsLessThan, new DateTime(2020, 03, 11));
Condition[] rcdConditions = new Condition[] { cdBeforeSpecificDate };
' Create conditions
Dim cdBeforeSpecificDate As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.IsLessThan, New DateTime(2020, 03, 11))
Dim rcdConditions As Condition() = New Condition() { cdBeforeSpecificDate }

From 12/24/2020 to 01/01/2021

A range of dates can be defined by creating two conditions. One condition defines the beginning of the date range by using the After or Is on or after operator. The other condition determines the end of the range. For this condition, the Before or Is on or before operator is used. Both conditions are connected by the And logical connective.

Image

To create these conditions programmatically, use the following code snippet:

// Create conditions
Condition cdFromDate = new Condition(m_dffDateFormField, Condition.ComparisonOperators.IsGreaterThanOrEqual, new DateTime(2020, 12, 24));
Condition cdToDate = new Condition(m_dffDateFormField, Condition.ComparisonOperators.IsLessThanOrEqual, new DateTime(2020, 01, 01));
Condition[] rcdConditions = new Condition[] { cdFromDate, cdToDate };
' Create conditions
Dim cdFromDate As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.IsGreaterThanOrEqual, New DateTime(2020, 12, 24))
Dim cdToDate As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.IsLessThanOrEqual, New DateTime(2020, 01, 01))
Dim rcdConditions As Condition() = New Condition() { cdFromDate, cdToDate }

Recurring events

Sometimes it is necessary to create conditions that consider recurring events. Such events could be specific dates in a year, such as birthdays, or not date specific events, where the date of the event is related to a specific year, month, week or weekday.

Some examples for recurring events are listed as follows:

December 25

A specific day of a specific month needs two conditions: A condition to determine a month and a condition to determine the day of the month. Both conditions have to be connected by an And logical connective.

Image

To create these conditions programmatically, use the following code snippet:

// Create conditions
Condition cdIsDecember = new Condition(m_dffDateFormField, Condition.ComparisonOperators.Is, Condition.Month.December);
Condition cdIs25th = new Condition(m_dffDateFormField, Condition.ComparisonOperators.Is, 25);
Condition[] rcdConditions = new Condition[] { cdIsDecember, cdIs25th };
' Create conditions
Dim cdIsDecember As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[Is], Condition.Month.December)
Dim cdIs25th As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[Is], 25)
Dim rcdConditions As Condition() = New Condition() { cdIsDecember, cdIs25th }

The 4th Thursday in November

To determine a specific weekday in a specific week of a specific month, four conditions have to be created that are joined by an And logical connective.

First, we have to create a condition that checks whether the date of the form field is in the specified month:

Image

The next condition determines that the date has to be on the requested weekday:

Image

The third and fourth one specifies the range of days of the month on which the weekday can be. To determine a weekday in the first week, the range has to be between the 1st and 7th day of the month. For the second week it has to be between the 8th and 14th, the third week is between the 15th and 21st and the fourth between the 22nd and 28th day of the month:

Image

To create these conditions programmatically, use the following code snippet:

// Create conditions
Condition cdIsNovember = new Condition(m_dffDateFormField, Condition.ComparisonOperators.Is, Condition.Month.November);
Condition cdIsThursday = new Condition(m_dffDateFormField, Condition.ComparisonOperators.Is, System.DayOfWeek.Thursday);
Condition cdIsOnOrAfter22nd = new Condition(m_dffDateFormField, Condition.ComparisonOperators.IsGreaterThanOrEqual, 22);
Condition cdIsOnOrBefore28th = new Condition(m_dffDateFormField, Condition.ComparisonOperators.IsLessThanOrEqual, 28);
Condition[] rcdConditions = new Condition[] { cdIsNovember, cdIsThursday, cdIsOnOrAfter22nd, cdIsOnOrBefore28th };
' Create conditions
Dim cdIsNovember As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[Is], Condition.Month.November)
Dim cdIsThursday As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[Is], DayOfWeek.Thursday)
Dim cdIsOnOrAfter22nd As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.IsGreaterThanOrEqual, 22)
Dim cdIsOnOrBefore28th As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.IsLessThanOrEqual, 28)
Dim rcdConditions As Condition() = New Condition() { cdIsNovember, cdIsThursday, cdIsOnOrAfter22nd, cdIsOnOrBefore28th }

Last Monday in May

The range of days to determine the last specific weekday of a month depends on the day number of the specified month. For those months that have 31 days, the range starts with the 25th day. Potential days for the last week of months with 30 days begin at the 24th of the month. The range of February starts at the 22nd (or 23rd, if February has 29 days) day of the month.

Image

To create these conditions programmatically, use the following code snippet:

Condition cdIsMonday = new Condition(m_dffDateFormField, Condition.ComparisonOperators.Is, System.DayOfWeek.Monday);
Condition cdIsMay = new Condition(m_dffDateFormField, Condition.ComparisonOperators.Is, Condition.Month.May);
Condition cdIsOnOrAfter25th = new Condition(m_dffDateFormField, Condition.ComparisonOperators.IsGreaterThanOrEqual, 25);
Condition[] rcdConditions = new Condition[] { cdIsMonday, cdIsMay, cdIsOnOrAfter25th };
' Create conditions
Dim cdIsMonday As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[Is], DayOfWeek.Monday)
Dim cdIsMay As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[Is], Condition.Month.May)
Dim cdIsOnOrAfter25th As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.IsGreaterThanOrEqual, 25)
Dim rcdConditions As Condition() = New Condition() { cdIsMonday, cdIsMay, cdIsOnOrAfter25th }

Weekend

To define the weekend, the date form field value has to be on a Saturday or a Sunday.

Image

To create these conditions programmatically, use the following code snippet:

// Create conditions
Condition cdIsSaturday = new Condition(m_dffDateFormField, Condition.ComparisonOperators.Is, System.DayOfWeek.Saturday);
Condition cdIsSunday = new Condition(m_dffDateFormField, Condition.ComparisonOperators.Is, System.DayOfWeek.Sunday) { LogicalConnective = Condition.LogicalConnectives.Or };
Condition[] rcdConditions = new Condition[] { cdIsSaturday, cdIsSunday };
' Create conditions
Dim cdIsSaturday As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[Is], DayOfWeek.Saturday)
Dim cdIsSunday As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[Is], DayOfWeek.Sunday) With {
    .LogicalConnective = Condition.LogicalConnectives.[Or]
}
Dim rcdConditions As Condition() = New Condition() { cdIsSaturday, cdIsSunday }

Weekday

Checking for weekdays can be defined in the same way as checking for dates on a weekend (using Monday, Tuesday, Wednesday, Thursday and Friday, except Saturday and Sunday). A more smart approach is determining all days except Saturday and Sunday by using the Is not operator and connecting both connections by the And logical connective.

Image

To create these conditions programmatically, use the following code snippet:

// Create conditions
Condition cdIsNotSaturday = new Condition(m_dffDateFormField, Condition.ComparisonOperators.IsNot, System.DayOfWeek.Saturday);
Condition cdIsNotSunday = new Condition(m_dffDateFormField, Condition.ComparisonOperators.IsNot, System.DayOfWeek.Sunday);
Condition[] rcdConditions = new Condition[] { cdIsNotSaturday, cdIsNotSunday };
' Create conditions
Dim cdIsNotSaturday As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[IsNot], DayOfWeek.Saturday)
Dim cdIsNotSunday As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[IsNot], DayOfWeek.Sunday)
Dim rcdConditions As Condition() = New Condition() { cdIsNotSaturday, cdIsNotSunday }

Each Monday in 2020

To define an event for a specific weekday in a specific year, an Is condition for the year and an Is condition for the weekday has to be defined. Both conditions are joined by an And logical connective.

Image

To create these conditions programmatically, use the following code snippet:

// Create conditions
Condition cdIs2020 = new Condition(m_dffDateFormField, Condition.ComparisonOperators.Is, 2020);
Condition cdIsMonday = new Condition(m_dffDateFormField, Condition.ComparisonOperators.Is, System.DayOfWeek.Monday);
Condition[] rcdConditions = new Condition[] { cdIs2020, cdIsMonday };
' Create conditions
Dim cdIs2020 As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[Is], 2020)
Dim cdIsMonday As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[Is], DayOfWeek.Monday)
Dim rcdConditions As Condition() = New Condition() {cdIs2020, cdIsMonday}

Each weekend in 2020

To check for weekends in a specific year, a combination of two separate weekdays in a year condition has to be created and connected by the Or logical connective. The first part checks whether the date form field value represents a date on a Saturday in 2020 and the second one tests for a Sunday in 2020.

Image

To create this condition programmatically, use the following code snippet:

// Create conditions
Condition cdIs2020 = new Condition(m_dffDateFormField, Condition.ComparisonOperators.Is, 2020) { LogicalConnective = Condition.LogicalConnectives.Or };
Condition cdIsSaturday = new Condition(m_dffDateFormField, Condition.ComparisonOperators.Is, System.DayOfWeek.Saturday);
Condition cdIsSunday = new Condition(m_dffDateFormField, Condition.ComparisonOperators.Is, System.DayOfWeek.Sunday);
Condition[] rcdConditions = new Condition[] { cdIs2020, cdIsSaturday, cdIs2020, cdIsSunday };
' Create conditions
Dim cdIs2020 As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[Is], 2020) With {
    .LogicalConnective = Condition.LogicalConnectives.[Or]
}
Dim cdIsSaturday As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[Is], DayOfWeek.Saturday)
Dim cdIsSunday As Condition = New Condition(m_dffDateFormField, Condition.ComparisonOperators.[Is], DayOfWeek.Sunday)
Dim rcdConditions As Condition() = New Condition() {cdIs2020, cdIsSaturday, cdIs2020, cdIsSunday}