udfGetNthDowInMonth
str udfGetNthDowInMonth (str, int, int)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfGetNthDowInMonth (strYmdHms, intOccurrence, intDayOfWeek)
intOccurrence = Min (5, Max (1, intOccurrence))                                                   ; Limit to 1..5 weeks.
intDayOfWeek = Min (6, Max (0, intDayOfWeek))                                                     ; Limit to 0..6 days.
intMonth = ItemExtract (2, strYmdHms, ":")                                                        ; Save for later check.
strYmdHms = ItemReplace (1, 3, strYmdHms, ":")                                                    ; Set the 01.mm.yyyy of month.
intFirstDay = (TimeJulianDay (strYmdHms) + 5) mod 7                                               ; Sunday=0
If intOccurrence == 1 && intDayOfWeek == intFirstDay Then Return TimeAdd (strYmdHms, "0:0:0")     ; Return if first day of month hits the rule.
strYmdHms = TimeAdd (strYmdHms, "0:0:" : ((7 + intDayOfWeek - intFirstDay) mod 7))                ; Add diff. days to the first occurrence.
If intOccurrence == 1 Then Return strYmdHms                                                       ; Return if this day hits the rule.
strYmdHms = TimeAdd (strYmdHms, "0:0:" : (7 * (intOccurrence - 1)))                               ; Add diff. weeks to hit the rule.
If intMonth != ItemExtract (2, strYmdHms, ":") Then strYmdHms = TimeSubtract (strYmdHms, "0:0:7") ; Fallback if necessary and subtract 7 days.
Return strYmdHms
;..........................................................................................................................................
; This user defined function "udfGetNthDowInMonth" returns the Nth occurence of the day of the week
; in the specified month, e. g. the second Tuesday.
;
; If the Nth day cannot be resolved in this month, then the first resp. last occurence of the day will be returned.
; Example: The fifth Thursday in July 2002 does not exist (actually it will be calculated to the 01.08.2002),
; so this function returns the last 'good' Thursday 25.07.2002.
;
; With intDayOfWeek in the range of 0..6 = Sunday..Saturday.
;
; Detlev Dalitz.20020720.20090728.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

strYmdHms1 = udfGetNthDowInMonth ("2002:07:05", 1, 1)   ; 1st Monday in Month July 2002 = "2002:07:01:00:00:00"
strYmdHms2 = udfGetNthDowInMonth ("2002:07:05", 4, 1)   ; 4th Monday in Month July 2002 = "2002:07:22:00:00:00"

strYmdHms3 = udfGetNthDowInMonth ("2002:07:05", 0, 1)   ; 1st Monday in Month July 2002 = "2002:07:01:00:00:00"
strYmdHms4 = udfGetNthDowInMonth ("2002:07:05", 10, 1)  ; 5th Monday in Month July 2002 = "2002:07:29:00:00:00"

strYmdHms5 = udfGetNthDowInMonth ("2002:07:05", 5, 0)   ; 5th Sunday in Month July 2002 = "2002:07:28:00:00:00"
strYmdHms6 = udfGetNthDowInMonth (TimeYmdHms (), 3, 3)  ; 3rd Wednesday in current Month, e. g ."2009:07:15:14:01:10"

Exit