Board index » delphi » How to get the number of the week.

How to get the number of the week.

Hi,

I'm currently making a little source on how to create the number of
week from the system date of MSDOS. I'm very curious to some other
solutions, so could someone give me a clue hoe to manage that??
Sources are very welcome.

Regards Ronald

 

Re:How to get the number of the week.


rva...@xs4all.nl (Ronald Van Aken) wrote:

Quote
>Hi,
>I'm currently making a little source on how to create the number of
>week from the system date of MSDOS. I'm very curious to some other
>solutions, so could someone give me a clue hoe to manage that??
>Sources are very welcome.
>Regards Ronald

I have developed a unit containing a collection of calender related
routines, including conversions from an (internal) date format to the
week number. My program uses the number of days since 1600-01-01 as
the basis for the day number. The following code (which contains some
other routines (with quite meaningful names) ) generate a week number
for the specified day. Please keep in mind that the related year does
not have to be the same as the year for the day number! For instance
sunday 1995-01-01 is actually week 1994-52!
The calcultaion given here is the standardized European week number,
where the weeks start with Monday and weeks are not split into parts.

The code fragment follows:

Procedure _DayNrToXYearWeek(DayNr: LongInt; Var XYear: Word; Var Week:
Word);
    {DayNr is normalized here}
    Var
      Year: Word;
      Month: Word;
      Day: Word;
      DayNr0101: LongInt;
      DayInWeek0101: Word;
      DayNr1: LongInt;
    Begin
    If DayNr=CachedDayNr3 Then
      Begin
      End
    Else
      Begin
      _DayNrToYearMonthDay(DayNr,Year,Month,Day);
      DayNr0101:=_YearMonthDayToDayNr(Year,1,1);
      DayInWeek0101:=_DayNrToDayInWeek(DayNr0101);
      If DayInWeek0101 In [1..4] Then
        Begin
        DayNr1:=DayNr0101-_DayNrToDayInWeek(DayNr0101)+1;
        End
      Else
        Begin
        DayNr1:=DayNr0101-_DayNrToDayInWeek(DayNr0101)+8;
        End;
      If DayNr<DayNr1 Then
        Begin
        DayNr0101:=DayNr0101-NumberOfDaysInYear(Year-1);
        CachedXYear3:=Year-1;
        DayInWeek0101:=_DayNrToDayInWeek(DayNr0101);
        If DayInWeek0101 In [1..4] Then
          Begin
          DayNr1:=DayNr0101-_DayNrToDayInWeek(DayNr0101)+1;
          End
        Else
          Begin
          DayNr1:=DayNr0101-_DayNrToDayInWeek(DayNr0101)+8;
          End;
        CachedWeek3:=1+((DayNr-DayNr1) Div 7);
        End
      Else
        Begin
        CachedWeek3:=1+((DayNr-DayNr1) Div 7);
        CachedXYear3:=Year;
        If CachedWeek3=53 Then
          Begin
          DayNr0101:=DayNr0101+NumberOfDaysInYear(Year);
          DayInWeek0101:=_DayNrToDayInWeek(DayNr0101);
          If DayInWeek0101 In [1..4] Then
            Begin
            DayNr1:=DayNr0101-_DayNrToDayInWeek(DayNr0101)+1;
            End
          Else
            Begin
            DayNr1:=DayNr0101-_DayNrToDayInWeek(DayNr0101)+8;
            End;
          If DayNr>=DayNr1 Then
            Begin
            CachedWeek3:=1;
            CachedXYear3:=Year+1;
            End;
          End;
        End;
      End;
    XYear:=CachedXYear3;
    Week:=CachedWeek3;
    End; {_DayNrToXYearWeek}

Re:How to get the number of the week.


rva...@xs4all.nl (Ronald Van Aken) wrote:

Quote
>I'm currently making a little source on how to create the number of
>week from the system date of MSDOS. I'm very curious to some other
>solutions, so could someone give me a clue hoe to manage that??
>Sources are very welcome.

Hmmm... Just off the top of my head...
You should be able to find the day of the year (a short routine to
add in the number of days in each month) and divide by seven,
then truncate.

If you want the week to start on Sunday (i.e. not just have the
number of seven-day periods that have passed) then you would
need to find the day of the week for Jan 1 of that year and then
subtract that number of days off the total (or add enough days
to make a complete week if you want to count the first partial
week as a full week).

Hope that helps. If you like, e-mail me and I'll try and send you
some source.

Alan

Alan Hohn -- AMH...@po.ASM-Intl.org
ASM International
Materials Park, OH  44073
My opinions, not ASM's.

Other Threads