View Thread

The following is the text of the current message along with any replies.
 Messages 1 to 10 of 10 total
 Anyone have a WeekOfMonth function
 Sat, Sep 12 2015 4:58 PM Permanent Link Trinione Hi: Does anyone have a WeekOfMonth function they are willing to share? Sat, Sep 12 2015 4:58 PM Permanent Link Trinione That works with EWB of course. Sun, Sep 13 2015 3:27 AM Permanent Link squiffy Telemix Ltd. Out of curiosity, what would you consider the week of a month? What I mean is, would you start from the 1st no matter what day fo the week it was, or would you start from the first Sunday/Monday? Sun, Sep 13 2015 4:48 AM Permanent Link squiffy Telemix Ltd. Something like this? function getWeekOfMonth:Integer; var  iStart,iEnd,iYear,iMonth,iLoop: Integer;  startDate: DateTime;  iWeekCount:integer; begin  // Parametise the function for other year/month settings.  iYear:=YearOf(Date);  iMonth:=MonthOf(Date);  // Get the first of month date.  startDate:=EncodeDate(iYear,iMonth,1);  // Get the day of the week for the 1st of the month.  iStart:=WeekDayOf(startDate);  // Get the current day of the month.  iEnd:=DayOf(Date);                                // We start in week 1 always.  iWeekCount:=1;  for iLoop:=iStart to iEnd do  begin     // Inc week count if you pass through a sunday.     // Days of week are 1 (monday) to 7 (sunday).     if iLoop mod 8 = 0 then Inc(iWeekCount);  end;  Result:=iWeekCount; end; Sun, Sep 13 2015 4:49 AM Permanent Link squiffy Telemix Ltd. Not used in battle - I just wrote that to get over writer's block ... I'm sure there's a more elegant, mathematical way to work it out. Sun, Sep 13 2015 11:41 AM Permanent Link Trinione Thanks. I will check it in a few minutes. Using this month, Sep-2015 as an example, there are 5 weeks, with the week starting on a Sunday as it is for a Calendar grid. Sun, Sep 13 2015 3:46 PM Permanent Link squiffy Telemix Ltd. Sorry that code was wrong. This works better i think : function getWeekOfMonth:Integer; var  iYear,iMonth,iDay : Integer;  iDayOfWeek,iRequiredDay : Integer;  iWeekCount : integer;  startDate : DateTime; begin  // Parametise the function for other year/moth settings.  iYear:=YearOf(Date);  iMonth:=MonthOf(Date);  iDay:=1;  // Get the first of month date.  startDate:=EncodeDate(iYear,iMonth,iDay);  // Get the day of the week for the 1st of the month.  iDayOfWeek:=WeekDayOf(startDate);  // Get the current day of the month.  iRequiredDay:=DayOf(Date);  // We start in week 1 always.  iWeekCount:=1;  while iDay < iRequiredDay do  begin    Inc(iDay);    Inc(iDayOfWeek);    if iDayOfWeek mod 7 = 0 then Inc(iWeekCount);  end;  Result:=iWeekCount; end; Sun, Sep 13 2015 5:10 PM Permanent Link Trinione That did not work. Here is code I got to work. Working with last day of week instead. function getWeekOfMonth(DateValue: Datetime) :Integer; var  iStart,iEnd,iYear,iMonth: Integer;  startDate: DateTime;  iWeekCount:integer;  iLastDayOfWeek: integer;  iFirstLastWeekday: integer;  iFullWeeks, iEndDay: integer; begin  // Parametise the function for other year/month settings.  iYear := YearOf(DateValue);  iMonth := MonthOf(DateValue);  // Set last day of week 1=Monday...7=Sunday  iLastDayOfWeek := 6;  // Get the first of month date.  startDate := EncodeDate(iYear,iMonth,1);  // Get the day of the week for the 1st of the month.  iStart := WeekDayOf(startDate);  // Get the current day of the month.  iEnd := DayOf(DateValue);    // Get the day of the end date  iEndDay := WeekDayOf(DateValue);  // We start in week 1 always.  iWeekCount := 1;  // get first iFirstLastWeekday  iFirstLastWeekday := iLastDayOfWeek - iStart + 1;    // get Full Weeks until iEnd    if iEnd < iFirstLastWeekday then      iFullWeeks := 0    else      iFullWeeks := (iEnd - iFirstLastWeekday) div 7;  // week count  iWeekCount := iWeekCount + iFullWeeks;                                    // if week does not end on iLastDayOfWeek  if iFullWeeks > 0 then  begin    if iEndDay <> iLastDayOfWeek then      Inc(iWeekCount);  end  else  begin    if iEnd > iFirstLastWeekday then      Inc(iWeekCount);  end;  result := iWeekCount; end; Tue, Sep 15 2015 10:20 AM Permanent Link squiffy Telemix Ltd. Glad you've got a working function. It's hardly important, but if you have a spare minute I'm curious about what didn't work on mine (the second code, not the first one)? I just ran it for a whole month and each day was in what I thought was the correct week. Cheers, Tue, Sep 15 2015 1:11 PM Permanent Link Trinione << It's hardly important, but if you have a spare minute I'm curious about what didn't work on mine (the second code, not the first one)? I just ran it for a whole month and each day was in what I thought was the correct week. >>> No matter the date, it returned '3'.