Board index » delphi » Date to Week Number

Date to Week Number

Hi,

  I need an algorithm that can accept a given date in dd/mm/yyyy format and
return the week number of that date. (Say, I put 01/01/1998 in, it should
return 1, and if I put 08/01/1998 in, it should return 2.. etc).

  I tried various things myself but what I ended up with only worked for the
month January =] The rest of the months it returned either a number that was
too high, or too low.

--
Bernhard van Staveren        ----       Mungled headers, addresses below.
home: madcat(at)superunknown.ml.org     work: staveren(at)xirian.demon.nl
"Linux, WinNT and MS-DOS. The Good, The Bad and The Ugly"

 

Re:Date to Week Number


Since posting anything very useful is prohibited in this
group, i've emailed you a small unit that does the job
using julian dates.  Hope it helps.

--------------------------------------

On 4 Feb 1998 17:39:25 GMT, Bernhard van Staveren

Quote
<mad...@xirian.demon.nl> wrote:
>Hi,

>  I need an algorithm that can accept a given date in dd/mm/yyyy format and
>return the week number of that date. (Say, I put 01/01/1998 in, it should
>return 1, and if I put 08/01/1998 in, it should return 2.. etc).

>  I tried various things myself but what I ended up with only worked for the
>month January =] The rest of the months it returned either a number that was
>too high, or too low.

Re:Date to Week Number


In article <6ba94d...@xirian.demon.nl>,
Bernhard van Staveren  <mad...@xirian.demon.nl> wrote:
:  I need an algorithm that can accept a given date in dd/mm/yyyy format and
:return the week number of that date. (Say, I put 01/01/1998 in, it should
:return 1, and if I put 08/01/1998 in, it should return 2.. etc).

Amply covered in

 132960 Dec 13 1997 ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip
 tsfaqp.zip Common Turbo Pascal Questions and Timo's answers

   All the best, Timo

....................................................................
Prof. Timo Salmi   Co-moderator of news:comp.archives.msdos.announce
Moderating at ftp:// & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/>  ; FIN-65101,  Finland

Spam foiling in effect.  My email filter autoresponder will return a
required email password to users not yet in the privileges database.

Re:Date to Week Number


Quote
Bernhard van Staveren wrote in message <6ba94d...@xirian.demon.nl>...
>  I need an algorithm that can accept a given date in dd/mm/yyyy format and
>return the week number of that date. (Say, I put 01/01/1998 in, it should
>return 1, and if I put 08/01/1998 in, it should return 2.. etc).

Hi....don't have any ready-to-use code for you, but I have an idea you can
work with.  First convert the date into a julian date (Jan 1=001, Feb 1=32,
etc) then integer divide:     ( ( date - 1 ) div 7 ) + 1

As for converting to julian, short of the plain ol' hard way, good luck!  :)

Re:Date to Week Number


JRS:  In article <6ba94d...@xirian.demon.nl> of Wed, 4 Feb 1998 17:39:25
in comp.lang.pascal.borland, Bernhard van Staveren

Quote
<mad...@xirian.demon.nl> wrote:
>  I need an algorithm that can accept a given date in dd/mm/yyyy format and
>return the week number of that date. (Say, I put 01/01/1998 in, it should
>return 1, and if I put 08/01/1998 in, it should return 2.. etc).

>  I tried various things myself but what I ended up with only worked for the
>month January =] The rest of the months it returned either a number that was
>too high, or too low.

What's a good Dutchman doing with a date format that's not ISO !!! <g> -
YYYY MM DD please !!!

Be careful that the correct definition of week number for your
particular purpose is used; ISTM that at least one previous respondent
has neglected this point.  Sometimes Week 1 is taken as that containing
Jan 1, even if that is a Saturday; sometimes Week 1 is taken as the
first week containing at least 4 days of the week;;  the week can be
taken as Sun..Sat, Mon..Sun, and other possibilities.  AFAIR, there is
an ISO standard that applies; there may also be an EU one and a Dutch
one - which ought to be the same.

(Fx : now looks up sone facts ...)

Consider TSFAQP #92 - but I don't follow all of it.

See URL: http://www.pip.dknet.dk/~c-t/calendar.html and in
news:news.answers, The Calendar FAQ, Section 5.6 :

  5.5. What Is the First Day of the Week?
  ---------------------------------------
  It is common Jewish and Christian practice to regard Sunday as the
  first day of the week. However, the fact that, for example, Russian
  uses the name "second day" for Tuesday, indicates that some nations
  regard Monday as the first day.
  In international standard IS-8601 the International Organization for
  Standardization (ISO) has decreed that Monday shall be the first day
  of the week.

  5.6. What Is the Week Number?
  -----------------------------
  International standard IS-8601 (mentioned in section 5.5) assigns a
  number to each week of the year. A week that lies partly in one year
  and partly in another is assigned a number in the year in which most
  of its days lie. This means that
          Week 1 of any year is the week that contains 4 January,
  or equivalently
          Week 1 of any year is the week that contains the first
          Thursday in January.
  Most years have 52 weeks, but years that start on a Thursday and leap
  years that start on a Wednesday have 53 weeks.

Timo : TSFAQP #92 A2 could be amended to refer to Calfaq 5.2, by
replacing Sentence 4.  S4 was, no doubt, correct when written; but if
you now have read the above, it must no longer be so ...

The standard is ISO-8601.
                  ^

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
 Don't Mail News.  Y2k for beginners http://www.merlyn.demon.co.uk/year2000.txt
 My list of Critical Dates is now at http://www.merlyn.demon.co.uk/critdate.htm
 Web <URL: http://www.merlyn.demon.co.uk/> --- includes FAQqish topics & links.

Re:Date to Week Number


Quote
micha...@frontiernet.net (michael) wrote:
>Since posting anything very useful is prohibited in this
>group, i've emailed you a small unit that does the job
>using julian dates.  Hope it helps.

Sorry you feel that way Michael, but refusing to provide solutions
to homework assignments in cases where the student has obviously not
even attempted a solution does not mean that posting anything useful
is prohibited.

For more information on this topic see Timo's Pascal FAQ
ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip

Subject: Pascal homework on the net

23. *****
 Q: What is all this talk about "Pascal homework on the net"?

As you read the various topics in the FAQ, you'll see that quite a
bit of useful information is posted to these groups.

Quote

>--------------------------------------

>On 4 Feb 1998 17:39:25 GMT, Bernhard van Staveren
><mad...@xirian.demon.nl> wrote:

>>  I need an algorithm that can accept a given date in dd/mm/yyyy format and
>>return the week number of that date. (Say, I put 01/01/1998 in, it should
>>return 1, and if I put 08/01/1998 in, it should return 2.. etc).

Download Timo Salmi's FAQ for information on this topic and many
other frequently (and not so frequently) asked Pascal questions with
Timo's answers.

From Timo's FAQ  <ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip>

    ...red
--
Support the anti-Spam amendment
  Join at http://www.cauce.org/

From t...@uwasa.fi Fri Nov 7 00:01:32 1997
Subject: Week number

92. *****
 Q: Does anybody have a program that gives the week number?

 A1: The first part of the answer comes without source code just
with a pointer to a TPU including a week number algorithm. There is
a function
 "WEEKNRFN Returns the week number for a given date"
in the TSUNTE.TPU unit in my
 ftp://garbo.uwasa.fi/pc/ts/tspa3570.zip
 Turbo Pascal 7.0 real mode units for (real:-) programmers.
(The unit collection is also available for earlier TP versions.)

 A2: Below is with permissions the weeknumber source code by Anders
Roar Nielsen ar...@night.ping.dk posted to the Usenet newsgroup
news:comp.lang.pascal.borland by Mark Cole mc...@spuddy.mew.co.uk.
The DayNumber function has been streamlined by Dr. John Stockton.
Only apply on the Gregorian calendar is covered. I do not know if
weekday numbering is internationally standardized or if it is rather
based on national practices.

function FirstThursday (Year: Integer) : Integer;
  begin
    FirstThursday := 7 - (1 + (Year-1600) + (Year-1597) div 4
    - (Year-1501) div 100 + (Year-1201) div 400) mod 7;
  end;

function DayNumber (Day, Month, Year : Integer) : Integer;
  const
    DaysBeforeMonth : array [1..12] of Integer =
                   (0,31,59,90,120,151,181,212,243,273,304,334);
  begin
    DayNumber := DaysBeforeMonth[Month] + Day + Ord( (Month > 2) and
      (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0)) ) ;
  end;

function WeekNumber (Day, Month, Year : Integer ) : Integer;
  begin
    if (Month = 1) and (Day < FirstThursday(Year)-3) then
      WeekNumber := WeekNumber(31,12,Pred(Year))
    else
      if (Month = 12) and (Day > FirstThursday(Succ(Year))+27) then
      WeekNumber := 1
    else
      WeekNumber := (DayNumber(Day,Month,Year)-FirstThursday(Year)+10) div 7;
  end;
--------------------------------------------------------------------

Re:Date to Week Number


In article <6ba94d...@xirian.demon.nl>,
Bernhard van Staveren  <mad...@xirian.demon.nl> wrote:

Quote
>Hi,

>  I need an algorithm that can accept a given date in dd/mm/yyyy format and
>return the week number of that date. (Say, I put 01/01/1998 in, it should
>return 1, and if I put 08/01/1998 in, it should return 2.. etc).

How about these:

Function WDayx(y,m,d:word):byte;  {Mar 1, 1900..Feb 28 2100, Mo=1, Su=7}
Begin
  WDayx:=(y+y shr 2+d+2*m+9*m shr 4
         +ord(m<=2) shl (ord((y and 3>0)))) mod 7+1;
End;

Function WDay(y,m,d:word):byte;  {Mar 1, 1900..Feb 28 2100, Mo=1, Su=7}

const lut:array[1..12,boolean] of byte=
       ((4,3),(0,6),(0,0),(3,3),(5,5),(1,1),
        (3,3),(6,6),(2,2),(4,4),(0,0),(2,2));
Begin
  WDay:=(y+y shr 2+d+lut[m,y and 3=0]) mod 7+1;
End;

Osmo

Other Threads