Mega Code Archive

 
Categories / Delphi / Examples
 

How can i determine the week number of a given day in the year

Question: How can I determine the week number of a given day in the year? Answer: This is a highly debated subject, as the first week of the year may be considered to be before or after Jan 1. The following function provides a starting point for WeekOfYear calculations. Example: function WeekOfYear(ADate : TDateTime) : word; var day : word; month : word; year : word; FirstOfYear : TDateTime; begin DecodeDate(ADate, year, month, day); FirstOfYear := EncodeDate(year, 1, 1); Result := Trunc(ADate - FirstOfYear) div 7 + 1; end; procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage(IntToStr(WeekOfYear(Date))); end;