Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the week number from a date?

Status
Not open for further replies.

MattWoberts

Programmer
Oct 12, 2001
156
GB
Hi,

I have a date stored in a C++ tm structure, and I need to work out what the week number is. Weeks always start on a Sunday, so if for example the date 1-jan-2002 was on a tuesday, then week 1 starts on the preceeding Sunday (30-Dec-2001), thus 1-jan-2002 is in week 1, but the 6-jan-2002 is in week 2 etc.

Does anyone know of a good way to calculate the week number automatically?

Many thanks

Matt Roberts
 
Well... Aren't you mistaking?
The weeks are contained in months, which are contained in years, which start in no matter what day...

You generally refer to a week as "week 2 from April" or "50th week of the year".

The first week of the year is the first week of January...

But, that's just a thought, you might have strict specifications on that...

If not, just divide the number of days ellapsed to 7.. :) [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Thanks. Yes, we do have some rules for week numbers here, the first week does not always start on January 1st, but it can start on as odd a date as 28-December of the previous year.

I have discovered a funtion strftime, that does actually format a string in a variety of date formats, including week numbers, so I am hoping to use that to do the work for me :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top