Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Week Of The Year

12 views
Skip to first unread message

Steve Thompson

unread,
Aug 26, 1996, 3:00:00 AM8/26/96
to

Is there a routine that can calculate the week number in the year ?


Thanks.


Steve.


Erik Johnson

unread,
Aug 26, 1996, 3:00:00 AM8/26/96
to Steve Thompson

> Is there a routine that can calculate the week number in the year ?

If you can get your hands on a unit for Julian
dates you could subtract the date of the 1st of
the year from the current date and div by 52.

I could supply you with a unit that handles
julian dates if you cannot find one.

-Erik Johnson
er...@phidias.colorado.edu
http://phidias.colorado.edu/vgallery.html

Sven Pran

unread,
Aug 27, 1996, 3:00:00 AM8/27/96
to

Erik Johnson wrote:
>
> > Is there a routine that can calculate the week number in the year ?
>
> If you can get your hands on a unit for Julian
> dates you could subtract the date of the 1st of
> the year from the current date and div by 52.
. . . .

NO! That will not be correct!

Week 1 in any year is the first periode from Monday through Sunday which
includes at least 4 days of the new year, this is equivalent to say that
week 1 is the first week (Monday through Sunday) which includes January
4th.

So you have to calculate the number of days since the last Sunday
preceeding January 4th, add 6 and divide by 7.

Equivalent, but probably slightly easier programming is to calculate the
number of days since January 4th, add the day number (Monday=0,
Sunday=6) for January 4th, add 7 and divide the sum by 7.

Now if this calculation results in week number 0 then you actually have
the last week (could be either 52 or 53) of the previous year. (Perform
the same calculation for December 31st in the previous year!).

And if the calculation results in week number 53 then you have to check
if it is really week 53, or if it is week 1 in the following year.
(Perform the same calculation for January 1st in the following year!).

regards Sven

Brady Holt

unread,
Aug 27, 1996, 3:00:00 AM8/27/96
to

Steve Thompson wrote:
>
> Is there a routine that can calculate the week number in the year ?
>
> Thanks.
>
> Steve.

Man....Thats Part Of Programming! You've got to tell the program how
many weeks are in a year, how many days are in a week and what days are
on what date and what dates are in what weeks and what week number it
is. You've got to GIVE THE COMPUTER EVERYTHING so it knows what to do.
That routine wouldn't be that long but kinda a pain in the butt to
write. Just look at a calendar =)

Brady Holt

Leonard Erickson

unread,
Aug 29, 1996, 3:00:00 AM8/29/96
to

Sven Pran <Sven...@alcatel.no> writes:

> Week 1 in any year is the first periode from Monday through Sunday which
> includes at least 4 days of the new year, this is equivalent to say that
> week 1 is the first week (Monday through Sunday) which includes January
> 4th.
>
> So you have to calculate the number of days since the last Sunday
> preceeding January 4th, add 6 and divide by 7.

From some old posts:

<---------------------- cut here ---------------------->

> Some excerpts from ISO 2015-1976 (E) Numbering of weeks:
>
> 1. "For the purpose of week numbering, the first day of a week shall
> be Monday."
> 2. "Week number one of a year is the first week containing four days
> or more of the new year." "NOTE -- The first day of a week being
> Monday, week number one of a year is the week containing the first
> Thursday of January"
> 3. "For the purpose of automatic data processing, the week numbers one
> to nine will be written with two digits (i.e. 01 to 09)"
>
> From the examples given in the annex to the above standard,
> 1974-12-29 (Sunday) is the last day of week no. 52.
> 1974-12-30 (Monday) is the first day of week no. 01 of 1975
> ...
> 1975-12-28 (Sunday) is the last day of week no. 52 of 1975
> 1975-12-29 (Monday) is the first day of week no. 01 of 1976
> ...
> 1976-12-26 (Sunday) is the last day of week no. 52 of 1976
> 1976-12-27 (Monday) is the first day of week no. 53 of 1976
> ...
> 1977-01-02 (Sunday) is the last day of week no. 53 of 1976
> 1977-01-03 (Monday) is the first day of week no. 01 of 1977
> ...
> 1978-01-01 (Sunday) is the last day of week no. 52 of 1977
> 1978-01-02 (Monday) is the first day of week no. 01 of 1978
> ...
> 1978-12-31 (Sunday) is the last day of week no. 52 of 1978
> 1979-01-01 (Monday) is the first day of wekk no. 01 of 1979

ISO 8601:1988 superceded ISO standard 2015 amongst others but the week
numbering appears to be the same. A description of the standard is
available at

http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html
ftp://ftp.uni-erlangen.de/pub/doc/ISO/ISO8601.ps.Z

<---------------------- cut here ---------------------->

> Equivalent, but probably slightly easier programming is to calculate the
> number of days since January 4th, add the day number (Monday=0,
> Sunday=6) for January 4th, add 7 and divide the sum by 7.

Even easier is calculate which day of the week the 1st of January is,
then use a lookup table:

Week day for Jan 1
1 Monday
1 Tuesday
1 Wednesday
1 Thursday
0 Friday
0 Saturday
0 Sunday

Where "0" indicates that Jan 1 is in the *last* week of the previous year.

--
Leonard Erickson (aka Shadow)
sha...@krypton.rain.com <--preferred
leo...@qiclab.scn.rain.com <--last resort

Timo Salmi

unread,
Aug 31, 1996, 3:00:00 AM8/31/96
to

-From: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip Frequently Asked TP Questions
-Subject: Week number

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

A: This 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
112570 Aug 16 1994 ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip
tspa3470.zip Turbo Pascal 7.0 real mode units for (real:-) programmers.
(The unit collection is also available for earlier TP versions.)
--------------------------------------------------------------------

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
t...@uwasa.fi http://uwasa.fi/~ts BBS 961-3170972; FIN-65101, Finland

Mark Cole

unread,
Sep 4, 1996, 3:00:00 AM9/4/96
to

I have this old message laying around. I've never used the code and don't
know how well it works (don't blame me :-)

Area : PAS.DELPHI
Date : Mar 19 '96, 11:56
From : Lars Ladingkaer 115:4505/102
To : Erik Knol
Subj : Caculating the week number
哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪

Hello Erik.

Sunday March 17 1996 00:04, Erik Knol wrote to All:

EK> I have tried to find a routine to calculate the week number.

>====cut====
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
if (Month > 2) and (Year mod 4 = 0) and ((Year mod 100 <> 0)
or (Year mod 400 = 0)) then DayNumber:=DaysBeforeMonth [Month]+Day+1
else DayNumber:=DaysBeforeMonth [Month]+Day;
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;

function WeekDay ( Day, Month, Year : Integer ) : Integer;
begin
WeekDay:=(7+DayNumber(Day,Month,Year)-FirstThursday(Year)+3) mod 7;
end;
end.
>====cut====

Bests,
Mark.
--

* Meeeow ! Call Spuddy on (01268) 515441 for FREE mail & Usenet access *

Timo Salmi

unread,
Sep 6, 1996, 3:00:00 AM9/6/96
to

In article <kaXLyAvn...@spuddy.mew.co.uk>,
Mark Cole <mc...@spuddy.mew.co.uk> wrote:
:I have this old message laying around. I've never used the code and don't

:know how well it works (don't blame me :-)

: EK> I have tried to find a routine to calculate the week number.

:function WeekNumber (Day, Month, Year : Integer ) : Integer;

I have tested to code you posted. It seems to work ok and with a few
year-end special situations to produce exactly the same results for
each day at least in my test range between 1890 and 2101 as the code
by Associate Professor Seppo Pynnonen and yours truly in

112582 Aug 16 1994 ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip


tspa3470.zip Turbo Pascal 7.0 real mode units for (real:-) programmers.

I will include this source into the FAQ. Would you or anyone else
know where it originally comes from. I ask, since I would like to be
able to give the proper acknowledgment in our Turbo Pascal FAQ to
the original author.

Dr John Stockton

unread,
Sep 6, 1996, 3:00:00 AM9/6/96
to

In article <kaXLyAvn...@spuddy.mew.co.uk> of Wed, 4 Sep 1996
12:31:32 in comp.lang.pascal.borland, Mark Cole <mc...@spuddy.mew.co.uk>
wrote:

>
> if (Month > 2) and (Year mod 4 = 0) and ((Year mod 100 <> 0)
> or (Year mod 400 = 0)) then DayNumber:=DaysBeforeMonth [Month]+Day+1
> else DayNumber:=DaysBeforeMonth [Month]+Day;

OR?

DayNumber := DaysBeforeMonth[Month] + Day + Ord( (Month > 2) and
(Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0)) ) ;


I believe that it's easier if you move Jan & Feb to be months 13 & 14 of
the previous year before the rest of the calculation -
if Month<=2 then begin Dec(Year) ; Inc(Month, 12) end ;

Also, one needs to consider how far back in time the method is valid
for, which is country-dependent (or worse - ISTR that, at one time,
Finland used two calendars at once) - see calendar.faq, linked on URL
below.
--
John Stockton, Surrey, UK. J...@merlyn.demon.co.uk Turnpike v1.12 MIME
http://www.merlyn.demon.co.uk/

Timo Salmi

unread,
Sep 7, 1996, 3:00:00 AM9/7/96
to

In article <l+7FGEAO...@merlyn.demon.co.uk>,
Dr John Stockton <j...@merlyn.demon.co.uk> wrote:
:In article <kaXLyAvn...@spuddy.mew.co.uk> of Wed, 4 Sep 1996

:12:31:32 in comp.lang.pascal.borland, Mark Cole <mc...@spuddy.mew.co.uk>
:wrote:
:>
:> if (Month > 2) and (Year mod 4 = 0) and ((Year mod 100 <> 0)

:
:OR?


:
:DayNumber := DaysBeforeMonth[Month] + Day + Ord( (Month > 2) and
: (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0)) ) ;

John, yes, if it produces the same result. My colleague Associate
Professor Seppo Pynnonen and I went over the code that Mark found
and posted, and the results it produced tally. Although numerecial
experimentation is no proof getting the same results from two
independently thought out and written codes means that there is a
good probablilyty of the results being correct.

I have assimilated the code Mark found into the TP FAQ.

Dr John Stockton

unread,
Sep 8, 1996, 3:00:00 AM9/8/96
to

In article <50ru0o$a...@reimari.uwasa.fi> of Sat, 7 Sep 1996 16:40:40 in

comp.lang.pascal.borland, Timo Salmi <t...@reimari.uwasa.fi> wrote:
>In article <l+7FGEAO...@merlyn.demon.co.uk>,
>Dr John Stockton <j...@merlyn.demon.co.uk> wrote:
>:In article <kaXLyAvn...@spuddy.mew.co.uk> of Wed, 4 Sep 1996
>:12:31:32 in comp.lang.pascal.borland, Mark Cole <mc...@spuddy.mew.co.uk>
>:wrote:
>:>
>:> if (Month > 2) and (Year mod 4 = 0) and ((Year mod 100 <> 0)
>:> or (Year mod 400 = 0)) then DayNumber:=DaysBeforeMonth [Month]+Day+1

>:> else DayNumber:=DaysBeforeMonth [Month]+Day;
>:
>:OR?
>:
>:DayNumber := DaysBeforeMonth[Month] + Day + Ord( (Month > 2) and
>: (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0)) ) ;
>
>John, yes, if it produces the same result. ...

Yes, it must produce the same result, because it is a simple optimisation
(which, I suspect, the compiler will not do).
From if A then B := Ccccc + 1 else B := Ccccc
to B := Ccccc + Ord(A) ;

I have now, however, tested with BPW that the two agree within a
for Year := 0 to 4000 do for Month := 1 to 12 do for Day := 0 to 32
loop; so all is as well with mine as it is with Mark's.

=====

Timo, I expect you'll have got my brief message, but I'll enlarge
on it for others who may write :

I can see that there are E-mails waiting on punt-1.demon.co.uk for me,
and I can see attempts to deliver them to me here at merlyn; but they do
not arrive. From here, I can also see who sent them.

E-mail for me goes straight here if I am dialled-in, but usually it
goes to punt-1 or punt-2 for delivery later.

0 new messages