87. Does Date X Fall Between Dates Y & Z?
Description
Determine if a date falls between two given dates. Or, determine if a date falls within a given range.
Code
Using Date Variables:
FALSE
IF DateVar > LowerDate-d AND DateVar < UpperDate-d
TRUE
END IF
Predefined Range:
FALSE
IF DateVar > DATE OF( Day, Month, Year )
AND DateVar < DATE OF( Day, Month, Year )
TRUE
END IF
Example #1:
(was child born in marriage)
FALSE
IF ChildBirthDate-d >= MarriageDate-d
AND ChildBirthDate-d <= DivorceDate-d
TRUE
END IF
Example #2:
FALSE
// Range is Flag Day (6/14) - Fourth of July
IF DateVar >= DATE OF( 14, 6, YEAR OF( DateVar ) )
AND DateVar <= DATE OF( 4, 7, YEAR OF( DateVar ) )
TRUE
END IF
Example #3:
FALSE
// Range is Christmas Eve to New Year's Day
IF ( MONTH OF( DateVar ) = 12 AND DAY OF( DateVar ) >= 24 )
OR ( MONTH OF( DateVar ) = 1 AND DAY OF( DateVar ) = 1 )
TRUE
END IF
Example #4:
FALSE
// Range is Halloween to Valentine's Day
IF DateVar >= DATE OF( 31, 10, YEAR OF( DateVar ) )
AND DateVar <= DATE OF( 31, 12, YEAR OF( DateVar ) )
TRUE
ELSE IF DateVar >= DATE OF( 1, 1, YEAR OF( DateVar ) )
AND DateVar <= DATE OF( 14, 2, YEAR OF( DateVar ) )
TRUE
END IF
Explanation
These computations analyze a date to see if it falls within a given range. If it does, TRUE is returned. Otherwise FALSE is returned. The range can either be set by two date variables or directly within the computation using the DATE OF model.
The heart of the computation is the formula: DateVar > LowerDate-d AND DateVar < UpperDate-d. Or, if you wish to include the uppper and lower dates in the range: DateVar >= LowerDate-d AND DateVar <= UpperDate-d.
The first example checks to see whether a child was born in the marriage. We want to know if the child's birth was within the range of the marriage, or whether the date variable ChildBirthDate-d falls between the date variable MarriageDate-d as the lower limit of the range and the date variable DivorceDate-d as the upper limit. If the child's birth date falls within this range, TRUE is returned. Otherwise FALSE is returned.
The second example shows how we predefine a range within the computation. This example checks a date to see whether it falls between Flag Day and the Fourth of July. To construct the range-defining dates we use the DATE OF( day, month, year ) model. Flag Day is June 14, or DATE OF( 14, 6, YEAR OF( DateVar )). The Fourth of July is DATE OF( 4, 7, YEAR OF( DateVar )). Notice that we derive the year from the date being analyzed (DateVar or TODAY) using the YEAR OF model.
The last two examples take up the issue of ranges that cross the year boundary. These require additional logic since two different years must be accounted for. Example #3 can take a simplified approach since the range only takes in two months. This computation checks whether a date falls between Christmas Eve and New Year's Day. Rather than comparing dates, it simply checks whether the date is in the months of December or January. If in December, it checks whether the day is >= 24. If in January, it checks whether the day = 1. Taking this approach we skirt the year issue altogether.
Example #4 involves a broad range that spans the year boundary: Halloween to Valentine's Day. For this computation we break the range into two: 10/31 - 12/31, and 1/1 - 2/14. Both ranges use the year of the date being analyzed, YEAR OF( DateVar ). If DateVar falls into either of those two ranges, the computation will return TRUE. Otherwise it will return FALSE.