Convert Date to Calenderweek (CW) and Calenderweek to Date
Posted: Thu Jan 24, 2008 4:12 pm
Here it is:
Feel free to use or optimize.
Code: Select all
Procedure.l ConvertCalenderWeekToDate(week.s)
; Function to convert a calenderweek expression (WW/YYYY) into a date (Monday of the week)
Protected year.w, cw.b, tdate.l, fdate.l
cw = Val(Left(week,2))
year = Val(Right(week,4))
fdate = Date(year,1,1,0,0,0)
If DayOfWeek(fdate) > 0 And DayOfWeek(fdate) < 5 ; The first week is the one, with min. 4 days in the new year
cw - 1
EndIf
ProcedureReturn AddDate(AddDate(fdate,#PB_Date_Day,(DayOfWeek(fdate)-1)*-1),#PB_Date_Week,cw)
EndProcedure
Procedure.s ConvertDateToCalenderWeek(date.l)
; Function to convert a date into a calenderweek expression (WW/YYYY)
Protected fdate.l, days.l, week.l
fdate = Date(Val(FormatDate("%yyyy",date)),1,1,0,0,0)
days = DayOfYear(date)-1
If DayOfWeek(fdate) > 0 And DayOfWeek(fdate) < 5 ; The first week is the one, with min. 4 days in the new year
days + DayOfWeek(fdate)-1
Else
days - (6 - DayOfWeek(fdate))
EndIf
week = days / 7
If days % 7 > 0
week + 1
EndIf
If week < 10
ProcedureReturn "0" + Str(week) + "/" + FormatDate("%yyyy",date)
Else
ProcedureReturn Str(week) + "/" + FormatDate("%yyyy",date)
EndIf
EndProcedure
; TEST
Debug FormatDate("%dd.%mm.%yyyy",ConvertCalenderWeekToDate("05/2009"))
Debug ConvertDateToCalenderWeek(Date(2010,05,22,0,0,0))