udfWeekOfMonth
int udfWeekOfMonth (str, int)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfWeekOfMonth (strYmdHms, intMode)
intDay = ItemExtract (3, strYmdHms, ":")
intDow = (7 + TimeJulianDay (strYmdHms) - intDay - intMode) mod 7
If !intDow Then intDow = 7
Return 1 + ((intDay + intDow - 2) / 7)
;..........................................................................................................................................
; This user defined function "udfWeekOfMonth" returns the number of the week
; within the month which is given by the first parameter datetime string strYmdHms.
;
; The second parameter intMode controls whether a week begins with Sunday or Monday.
; intMode = 0 ... Week begins with Sunday.
; intMode = 1 ... Week begins with Monday (conform with ISO 8601).
;
; The return value is an integer number in the range 1..6.
;
; Detlev Dalitz.20030819.20090726.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfWeekOfMonth_2 (strYmdHms)
Return 1 + udfWeekOfYear (strYmdHms) - udfWeekOfYear (ItemReplace (1, 3, strYmdHms, ":"))
; The return value is an integer number in the range 1..6.
; Monday is the first day of the week (conform to ISO 8601).
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfWeekOfYear (strYmdHms)
intJulianNow = TimeJulianDay (strYmdHms)
intJulianJan4 = TimeJulianDay (ItemExtract (1, strYmdHms, ":") : ":1:4")
intJulianDowJan4 = (intJulianJan4 + 5) mod 7 ; Range=0..6 = Sunday..Saturday
intJulianMonday = 1 + intJulianJan4 - intJulianDowJan4 - (7 * !intJulianDowJan4)
intWeek = (intJulianNow - intJulianMonday) / 7
intWeek = intWeek + (intJulianNow >= intJulianMonday)
Return intWeek
;..........................................................................................................................................
; The return value is an integer number in the range 0..53.
;
; ISO 8601 defines Monday as the first day of any week.
; Week W01 of any year is the first week that contains January 4.
; In some years January 1, 2 and 3 could fall into week W52 or W53 of the previous year.
; The last few days of December could fall into week W01 of the following year.
;
; Detlev Dalitz.20010325.20090726.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

; WeekOfMonth
intWeekOfMonth01 = udfWeekOfMonth (TimeYmdHms (), 0) ; Sunday "2009:07:26" ==> 5
intWeekOfMonth02 = udfWeekOfMonth (TimeYmdHms (), 1) ; Sunday "2009:07:26" ==> 4
intWeekOfMonth03 = udfWeekOfMonth_2 (TimeYmdHms ())  ; Sunday "2009:07:26" ==> 4

strYmdHms01 = "2002:12:29:00:00:00"
intWeekOfMonth01 = udfWeekOfMonth (strYmdHms01, 1) ; ==> 5

strYmdHms02 = "2002:12:30:00:00:00"
intWeekOfMonth02 = udfWeekOfMonth (strYmdHms02, 1) ; ==> 6

strYmdHms03 = "2003:01:05:00:00:00"
intWeekOfMonth03 = udfWeekOfMonth (strYmdHms03, 1) ; ==> 1

strYmdHms04 = "2003:01:06:00:00:00"
intWeekOfMonth04 = udfWeekOfMonth (strYmdHms04, 1) ; ==> 2

strYmdHms05 = "2003:01:07:00:00:00"
intWeekOfMonth05 = udfWeekOfMonth (strYmdHms05, 1) ; ==> 2

strYmdHms06 = "2003:08:01:00:00:00"
intWeekOfMonth06 = udfWeekOfMonth (strYmdHms06, 1) ; ==> 1

strYmdHms07 = "2003:08:17:00:00:00"
intWeekOfMonth07 = udfWeekOfMonth (strYmdHms07, 1) ; ==> 3

strYmdHms08 = "2003:08:18:00:00:00"
intWeekOfMonth08 = udfWeekOfMonth (strYmdHms08, 1) ; ==> 4

strYmdHms09 = "2003:08:25:00:00:00"
intWeekOfMonth09 = udfWeekOfMonth (strYmdHms09, 1) ; ==> 5

strYmdHms10 = "2004:05:01:00:00:00"
intWeekOfMonth10 = udfWeekOfMonth (strYmdHms10, 1) ; ==> 1

strYmdHms11 = "2004:05:02:00:00:00"
intWeekOfMonth11 = udfWeekOfMonth (strYmdHms11, 1) ; ==> 1

strYmdHms12 = "2004:05:03:00:00:00"
intWeekOfMonth12 = udfWeekOfMonth (strYmdHms12, 1) ; ==> 2

strYmdHms13 = "2004:05:30:00:00:00"
intWeekOfMonth13 = udfWeekOfMonth (strYmdHms13, 1) ; ==> 5

strYmdHms14 = "2004:05:31:00:00:00"
intWeekOfMonth14 = udfWeekOfMonth (strYmdHms14, 1) ; ==> 6

strYmdHms15 = "2004:02:29:00:00:00"
intWeekOfMonth15 = udfWeekOfMonth (strYmdHms15, 1) ; ==> 5

strYmdHms16 = "2013:08:19:00:00:00"
intWeekOfMonth16 = udfWeekOfMonth (strYmdHms16, 1) ; ==> 4

strYmdHms17 = "2013:12:30:00:00:00"
intWeekOfMonth17 = udfWeekOfMonth (strYmdHms17, 1) ; ==> 6

;   2013 Dezember
;        Mo Di Mi Do Fr Sa So
;   W48                     1  wom1
;   W49   2  3  4  5  6  7  8  wom2
;   W50   9 10 11 12 13 14 15  wom3
;   W51  16 17 18 19 20 21 22  wom4
;   W52  23 24 25 26 27 28 29  wom5
;   W53  30 31                 wom6

Exit