Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DateTimePicker/MonthCallender

Posted on 2002-07-30
6
Medium Priority
?
1,277 Views
Last Modified: 2009-12-16
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
Comment
Question by:sa9824
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7188132
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
 
LVL 17

Expert Comment

by:geobul
ID: 7188156
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
 
LVL 17

Expert Comment

by:geobul
ID: 7188190
Forgot to say that my example calculates weeks starting with Sundays.

Regards, Geo
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Accepted Solution

by:
aubs earned 800 total points
ID: 7188476
... 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
 
LVL 1

Expert Comment

by:plaster
ID: 7192974
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
 

Author Comment

by:sa9824
ID: 7195544
Thank You!! Workes like a clock!!!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question