udfGetCountDowInMonth
int udfGetCountDowInMonth (str, int)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfGetCountDowInMonth (strYmdHms, intDayOfWeek)                   ; With intDayOfWeek = 0..6 = Sunday..Saturday.
intDayOfWeek = Min (6, Max (-1, intDayOfWeek))                                    ; Limit intDayOfWeek to -1..0..6 days. -1 means current DayofWeek of strYmdHms.
If intDayOfWeek < 0 Then intDayOfWeek = (TimeJulianDay (strYmdHms) + 5) mod 7     ; 0..6 = Sunday..Saturday.
intMonth = Int (ItemExtract (2, strYmdHms, ":"))                                  ; Store intMonth for later check.
strYmdHms = ItemReplace ("1", 3, strYmdHms, ":")                                  ; Set the 01.mm.yyyy of month.
intDowFirst = (TimeJulianDay (strYmdHms) + 5) mod 7                               ; Sunday=0
strYmdHms = TimeAdd (strYmdHms, "0:0:" : (7 + intDayOfWeek - intDowFirst) mod 7)  ; Add diff. days to the first occurrence.
intCount = 0
While intMonth == Int (ItemExtract (2, strYmdHms, ":"))                           ; Still in this month?
   intCount = intCount + 1
   strYmdHms = TimeAdd (strYmdHms, "0:0:" : 7)                                    ; Add seven days.
EndWhile
Return intCount
;..........................................................................................................................................
; This Function "udfGetCountDowInMonth" returns the number of times
; the specified day of the week occurs in the month containing the given date.
;
; If the second parameter intDayOfWeek has the value of -1, then the function returns the number of times
; the weekday indicated by the first parameter strYmdHms occurs within its own month.
;
; Detlev Dalitz.20020720.20090728.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

; Test.

intCountDow1 = udfGetCountDowInMonth (TimeYmdHms (), -1) ; e. g. "2009:07:28:13:40:50" = 4 = Four Tuesdays in July 2009.
intCountDow2 = udfGetCountDowInMonth ("2002:07:31:00:00:00", 1) ; 5 = Five Mondays in July 2002.

Exit