Link to home
Start Free TrialLog in
Avatar of sa9824
sa9824Flag for Sweden

asked on

DateTimePicker/MonthCallender

Hi,

Is it possible get a weeknumber if you got a specifik date with the MonthCallender-component or the dateTimePicker-component? I use Delphi 5.

Avatar of kretzschmar
kretzschmar
Flag of Germany image

at
http://codecentral.borland.com/codecentral/ccweb.exe/listing?id=301

you will find this example

---begin paste

No that code will not work correctly. Here is code that does:

(Note that you must cater for days 'belonging' to week 52 or 53 in the
previous year and days belonging to week 1 in the next year. By international
standards week 1 is always the first week (Monday through Sunday) containing
at least 4 days of the year, this rule is equivalent to the first week with a
Thursday. It also implies that January 4th is always in week 1).

Be aware that the code includes no validation of the input.

{*************************************************}

Function LeapYear(year:word):boolean;
  { returns true if year is a leap year }

begin
 LeapYear := (((year mod 4) = 0) and
              (((year mod 100) <> 0) or ((year mod 400) = 0)));
end;

Function Jan4th(year:word):byte;
  { calculates the day of week for January 4th: Monday = 0 through Sunday = 6
}
var z : word;
begin
 z := (year-1) mod 400;
 z := z - ((z div 100) * 12);
 Jan4th := (((z+25) mod 28) * 5) div 4) mod 7;
end;

Function Week53inYear(year:word):boolean;
 { returns true if year has 53 weeks }
begin
 Week53inYear := (Jan4th(year) = 6) or
                 ((Jan4th(year) = 5) and LeapYear(year));
end;

Function DayOfYear(day,month,year:word):word;
 { calculates day number within year from dd,mm,yyyy }
const m : array[1..12] of word = (0,31,59,90,120,151,181,212,243,273,304,334);
var q : byte;
begin
 if (LeapYear(year) and (month > 2)) then q := 1 else q := 0;
 DayOfYear := day + m[month] + q;
end;

Function DayOfWeek(day,month,year:word):byte;
 { calculates the day of week (Monday through Sunday) from dd,mm,yyyy }
begin
 DayOfWeek := (DayOfYear(day,month,year) + 3 + Jan4th(year)) mod 7;
end;

Procedure WeekOfYear(var week, wyear : word; day, month,year:word);
  { calculates the week number and associated year for any given date }
begin
 week := (DayOfYear(day,month,year) + Jan4th(year) + 3) mod 7;
 case week of
   0 : begin
        wyear := year - 1;
        if Week53inYear(wyear) then week := 53 else week := 52;
       end;
  53 : begin
        if Week53inYear(year) then wyear := year
         else
          begin
           wyear := year + 1;
           week := 1;
          end;
       end;
  else wyear := year;
  end;
end;

{*********************************************************}

Regards Sven

--- end paste

hope this helps

meikl ;-)
Avatar of geobul
geobul

Hi,

function WeekOfYear(ADate : TDateTime) : integer;
var
  day : word;
  month : word;
  year : word;
  FirstOfYear : TDateTime;
begin
  DecodeDate(ADate, year, month, day);
  FirstOfYear := EncodeDate(year, 1, 1);
  FirstOfYear := FirstOfYear - DayOfWeek(FirstOfYear) + 1;
  Result := Trunc(ADate - FirstOfYear) div 7 + 1;
end;

// usage:
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(IntToStr(WeekOfYear(DateTimePicker1.Date)));
end;

Regards, Geo
Forgot to say that my example calculates weeks starting with Sundays.

Regards, Geo
ASKER CERTIFIED SOLUTION
Avatar of aubs
aubs

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Not sure if the DateUtils unit is available with Delphi 5 but thith Delphi 6 you can add Dateutils to the Uses.
then something like :-
label1.Caption:=inttostr(weekoftheyear(dtp1.DateTime));

Plaster
Avatar of sa9824

ASKER

Thank You!! Workes like a clock!!!