Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1294
  • Last Modified:

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.

0
sa9824
Asked:
sa9824
1 Solution
 
kretzschmarCommented:
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 ;-)
0
 
geobulCommented:
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
0
 
geobulCommented:
Forgot to say that my example calculates weeks starting with Sundays.

Regards, Geo
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
aubsCommented:
... and my version:

//gets a datecode. Returns year and weeknumber in format: YYWW
function getWeekNumber(today: Tdatetime): string;

{Delphi dayOfWeek function returns integer 1..7 equivalent to Sunday..Saturday.
ISO 8601 weeks start with Monday and the first week of a year is the one which
includes the first Thursday - Fiddle takes care of all this}

const Fiddle : array[1..7] of Byte = (6,7,8,9,10,4,5);

var
     present, startOfYear: Tdatetime;
     firstDayOfYear, weekNumber, numberOfDays: integer;
     year, month, day: word;
     YearNumber: string;

begin
     present:= trunc(today); //truncate to remove hours, mins and secs
     decodeDate(present, year, month, day); //decode to find year
     startOfYear:= encodeDate(year, 1, 1);  //encode 1st Jan of the year

  //find what day of week 1st Jan is, then add days according to rule
     firstDayOfYear:= Fiddle[dayOfWeek(startOfYear)];

     //calc number of days since beginning of year + additional according to rule
     numberOfDays:= trunc(present - startOfYear) + firstDayOfYear;

     //calc number of weeks
     weekNumber:= trunc(numberOfDays / 7);

     //Format year, needed to prevent millenium bug and keep the Fluffy Spangle happy
     YearNumber:= formatDateTime('yyyy',present);

     YearNumber:= YearNumber + 'W';

     if weekNumber < 10 then
    YearNumber:= YearNumber + '0';//add leading zero for week

     //create datecode string
     result:= YearNumber + inttostr(weekNumber);

  if weekNumber = 0 then //recursive call for year begin/end...
    //see if previous year end was week 52 or 53
    result:= getWeekNumber(encodeDate(year - 1, 12, 31))

  else if weekNumber = 53 then
    //if 31st December less than Thursday then must be week 01 of next year
    if dayOfWeek(encodeDate(year, 12, 31)) < 5 then
    begin
      YearNumber:= formatDateTime('yyyy',encodeDate(year + 1, 1, 1));
      result:= YearNumber + 'W01';
    end;

end;


Regards

Aubs
0
 
plasterCommented:
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
0
 
sa9824Author Commented:
Thank You!! Workes like a clock!!!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now