Calculate Week Number from ccyy, mm, dd

  • Thread starter Michael R. Copeland
  • Start date
M

Michael R. Copeland

I need to determine a given date's week number, but my searches of
google only confuse and frustrate me: there are many apparent formulas
(which are pretty complex) and they all seem to require several
supporting libraries or subprograms. It doesn't seem to be all that
complex, so I assume I'm getting the wrong "hits" on my searches.
I have year (ccyy), month, and day, and I can derive the ordinal year
(yyddd) without difficulty, but I'm looking for a bit of C or C++ code
that will take either and return a value of 1-53 for the week in which
that date falls. Any thoughts? TIA
 
V

Victor Bazarov

Michael said:
I need to determine a given date's week number, but my searches of
google only confuse and frustrate me: there are many apparent formulas
(which are pretty complex) and they all seem to require several
supporting libraries or subprograms. It doesn't seem to be all that
complex, so I assume I'm getting the wrong "hits" on my searches.
I have year (ccyy), month, and day, and I can derive the ordinal
year (yyddd) without difficulty, but I'm looking for a bit of C or
C++ code that will take either and return a value of 1-53 for the
week in which that date falls. Any thoughts? TIA

Have you tried to simply divide your ordinal number by 7? And, please,
do not ask us to supply a piece of code that by your own admission
already exist in many variations on the Web. Get the simplest one and
adapt. If you run into problems while adapting the code to your needs,
read the FAQ 5.8 and follow its recommendations.

V
 
B

Ben Pfaff

I have year (ccyy), month, and day, and I can derive the ordinal year
(yyddd) without difficulty, but I'm looking for a bit of C or C++ code
that will take either and return a value of 1-53 for the week in which
that date falls. Any thoughts? TIA

How are you defining the week number? There are multiple
definitions, and some are easier to calculate than others.
 
M

Michael R. Copeland

I need to determine a given date's week number, but my searches of
Have you tried to simply divide your ordinal number by 7? And, please,
do not ask us to supply a piece of code that by your own admission
already exist in many variations on the Web. Get the simplest one and
adapt. If you run into problems while adapting the code to your needs,
read the FAQ 5.8 and follow its recommendations.

First, everything I've found is in Pascal (mostly from John Stockton,
who is always precise and complete, almost to a fault...). Second, I
remain confused about _what_ algorithm(s) to use. Perhaps the problem
I'm working on _is_ very complex, but I was hoping to find a library or
simple algorithm from which to start... 8<{{
 
M

Michael R. Copeland

I have year (ccyy), month, and day, and I can derive the ordinal year
How are you defining the week number? There are multiple
definitions, and some are easier to calculate than others.

Perhaps that's the problem: I may not know what I'm asking for. 8<{{
I thought - perhaps naively - that there was a fairly simple algotithm
around that would produce a "week #" value (I don't need precision or
some of the detail that the ISO and other options imply: I'm only trying
to determine which week sales of certain days are in.). I'm not a
mathematician or such, so I don't know what complexity I need to
use...but the whole problem _seems_ quite simple. However, I can't seem
to find the "root" information I might work from...
 
V

Victor Bazarov

Michael said:
First, everything I've found is in Pascal (mostly from John
Stockton, who is always precise and complete, almost to a fault...).
Second, I remain confused about _what_ algorithm(s) to use. Perhaps
the problem I'm working on _is_ very complex, but I was hoping to
find a library or simple algorithm from which to start... 8<{{

http://www.google.com/search?q=week+number+c++

V
 
B

Ben Pfaff

Perhaps that's the problem: I may not know what I'm asking for. 8<{{
I thought - perhaps naively - that there was a fairly simple algotithm
around that would produce a "week #" value (I don't need precision or
some of the detail that the ISO and other options imply: I'm only trying
to determine which week sales of certain days are in.). I'm not a
mathematician or such, so I don't know what complexity I need to
use...but the whole problem _seems_ quite simple. However, I can't seem
to find the "root" information I might work from...

Some define week 1 as starting on January 1. Then the week
number is (ddd - 1) / 7 + 1 (with integer arithmetic).

Some define week 1 as starting on the first Monday, or Sunday
(etc.) of the year, with days before that belonging to the last
week of the previous year. Then the week number needs to be
adjusted based on the day of the week on January 1.

None of this should be tricky.
 
J

James Kanze

Perhaps that's the problem: I may not know what I'm asking for. 8<{{
I thought - perhaps naively - that there was a fairly simple algotithm
around that would produce a "week #" value (I don't need precision or
some of the detail that the ISO and other options imply: I'm only trying
to determine which week sales of certain days are in.).

The algorithm isn't complicated, but it depends when you
consider week 1 to start. Is it the week containing January
1st, even if January 1st falls on the last day of the week.
(Note too that depending on where you are, the first day of the
week may be either Sonday or Monday.) ISO considers that the
week number is determined by the year in which Thursday occurs,
so if the year starts on a Friday, that Friday (and the
following Saterday and Sunday) are in week 0 (or according to
ISO, week 52 or 53 of the previous year).
I'm not a mathematician or such, so I don't know what
complexity I need to use...but the whole problem _seems_ quite
simple. However, I can't seem to find the "root" information
I might work from...

The problem isn't one of implementation, as much as one of
specification. For the implementation, get the tm for Jan. 1st
of the target year, use the tm_wday field in it to determine the
offset between it and the start of week 1, and use this offset
to adjust the tm_yday field of the target date, then divide by
7. About the only place where the algorithm will vary will be
in determining the offset, and in the handling of partial weeks.
(According to ISO, Dec. 31, 2007 is in week 1 of 2008.)
 
M

Michael R. Copeland

I need to determine a given date's week number, but my searches of
google only confuse and frustrate me: there are many apparent formulas
(which are pretty complex) and they all seem to require several
supporting libraries or subprograms. It doesn't seem to be all that
complex, so I assume I'm getting the wrong "hits" on my searches.
I have year (ccyy), month, and day, and I can derive the ordinal year
(yyddd) without difficulty, but I'm looking for a bit of C or C++ code
that will take either and return a value of 1-53 for the week in which
that date falls. Any thoughts? TIA

The solution was already there, right within the language:

strftime(..."%U"...);

It does the calculation I need, and I've verified some key dates in
several years. Thanks for everyone's comments... 8<}}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top