• Status: Solved
• Priority: Medium
• Security: Public
• Views: 761

# Calculating week number ?

I've made a Delphi function that counts the number of week using given date as a parameter. Is it possible to determine the last week's number? (Is it 52 or 53?) Is there any component or function available that would do this job?
0
tonitop
1 Solution

Commented:
The function below calculates the weeknumber of a given date. Notice that the weeknumber depends on what is the first day of the week.

function WeekNumber(TheDate: TDateTime): Word;
var year, month, day, dow: Word;
d: TDateTime;
begin
DecodeDate(TheDate, year, month, day);
d := EncodeDate(year, 1, 1);
dow := DayOfWeek(d) - 1;
Result := (Round(TheDate - d) + dow) div 7;
if dow <= 3 then
Inc(Result);
if dow <> 3 then
begin
if Result = 0 then
Result := WeekNumber(EncodeDate(year - 1, 12, 31))
else if Result = 53 then
Result := WeekNumber(EncodeDate(year + 1, 1, 1));
end;
end;

Epsylon.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.