Re: Calculate week number of given date

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Boris Nienke (nospam_at_nsonic.de)
Date: 02/24/04


Date: Tue, 24 Feb 2004 14:59:13 +0100

Some Delphi-Procedures... don't know - but some things may be easier with
C# :

function dateWeekOfYear(D: TDateTime): Integer; { Armin Hanisch }
const
  t1: array[1..7] of ShortInt = ( -1, 0, 1, 2, 3, -3, -2);
  t2: array[1..7] of ShortInt = ( -4, 2, 1, 0, -1, -2, -3);
var
  doy1,
  doy2 : Integer;
  NewYear : TDateTime;
begin
  NewYear:=dateBeginOfYear(D);
  doy1 := dateDayofYear(D) + t1[DayOfWeek(NewYear)];
  doy2 := dateDayofYear(D) + t2[DayOfWeek(D)];
  if doy1 <= 0 then
    Result := dateWeekOfYear(NewYear-1)
  else if (doy2 >= dateDayofYear(dateEndOfYear(NewYear))) then
    Result:= 1
  else
    Result:=(doy1-1) div 7+1;
end;

function dateBeginOfYear(D: TDateTime): TDateTime;
var
  Year,Month,Day : Word;
begin
  DecodeDate(D,Year,Month,Day);
  Result:=EncodeDate(Year,1,1);
end;

function dateDayOfYear(D: TDateTime): Integer;
begin
  Result:=Trunc(D-dateBeginOfYear(D))+1;
end;

Boris



Relevant Pages

  • Re: Whats new in Tiburon
    ... lambda Result:=0); ... function (x, y: TDateTime): TDateTime; ... spares us from all the pointer stuff you have to do in C to pass arguments by reference. ... I try to avoid 'var' at all costs in my programs. ...
    (borland.public.delphi.non-technical)
  • Re: In Delphi zwei Zeiten vergleichen
    ... > Hallo Christian, ... > dT: TDateTime; ... Zeit1,Zeit2: String; ...
    (de.comp.lang.delphi.misc)
  • Re: How to time the execution of a method?
    ... function TimeMethod(x: TMethod): TDateTime; ... var i, t, tot: Cardinal; ... Inc(tot, GetTickCount - t); ...
    (comp.lang.pascal.delphi.misc)
  • Re: Wochennummer bestimmen
    ... FirstOfYear: TDateTime; ... WeekOfYear is part of Delphi. ...
    (comp.lang.pascal.delphi.misc)