Solved

DateTimePicker/MonthCallender

Posted on 2002-07-30
6
1,228 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
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Accepted Solution

by:
aubs earned 200 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now