Solved

Week Calculating Based On What Day The Week Start.

Posted on 2006-06-13
8
245 Views
Last Modified: 2010-04-04
HI PLEASE I NEED SOME EXPERT HELP.........

I HAVE a big problem, and  I dont know how to do it..

I need a week number for a given date. For Example my calendar works as follows.
Sometimes the  WEEK STARTS on a Saturday, Sometimes The Week begins on a Friday.

Now for example if the Date is Between 7 Jan and 14 Jan 2006 the week number must be 1 (this if if the week starts on a saturday)
If the date is between Feb 4 and Feb 11 2006  the week number must be 5 (also week starts on saturday)
If the date is between Feb 3 and Feb 10 2006  the week number must be 5 (NOW WEEK STARTS ON A FRIDAY)
OR If the date is between March 31 and April 7 2006  the week number must be 13 (NOW WEEK STARTS ON A FRIDAY)

Please how do i calculate the week number depending on what day of the week it starts. MY first week in the Year starts on a Saturday
or Friday. eg. for 2006 the first week must be Jan 6 - Jan 13 and week 52 must be Dec 29 - Jan 5 (Friday)

For 2007  first week will be Jan 5 - Jan 12 is firstweek (friday)

If week starts on Saturday then  2006  first week will be Jan 7 - 14 Jan
and week 52 will be Dec 31 - Jan 7


Thank you  VERY MUCH

0
Comment
Question by:henryreynolds
  • 4
  • 3
8 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 16893944
hi.

here is the function and test app:

function myweekofyear(dat:tdatetime; dayofweekstart:byte):byte;
var dd:word;
begin
  dd:=DaysBetween(StartOfTheYear(dat),dat)+1;
  dd:=dd-dayofweekstart+1;
  result:=(dd div 7)+1;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  showmessage(inttostr(myweekofyear(strtodate('03/31/2006'),5)));
end;
0
 

Author Comment

by:henryreynolds
ID: 16894378
Ciuly THANK YOU, THANK YOU,
Really i dont know how to thank you, This is working perfect YOU earn all your points

Thanx

Henry

South Africa
0
 

Author Comment

by:henryreynolds
ID: 16894442
Sorry Ciuly you impressed me so much, for the same principal now
can I turn this also around and pass the week again and get the Satuday Date or Friday Date
for that week, how can I do it the other away around.

eg i pass 23/2006 then it must return Jun 10 - Jun 17 for saturdays
and  23/2006 Jun 9 - Jun 16 for fridays

THANK YOU YOUR ARE THE BEST

henry
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 28

Expert Comment

by:2266180
ID: 16895107
I think there is an error in my initial code. there is a 6 day shift I am correct (didn't investigated too much).
here is the corrected code for that, and the one for your second request:
function myweekofyear(dat:tdatetime; dayofweekstart:byte):byte;
var dd:word; ddd:tdatetime;
begin
  ddd:=StartOfTheYear(dat);
  dd:=DaysBetween(ddd,dat);
  while DayOfWeek(ddd) <> dayofweekstart do
  begin
    ddd:=IncDay(ddd);
    dec(dd);
  end;
  result:=(dd div 7)+1;
end;

function mydateforweek(week:byte; year:word; dayofweekstart:byte):TDateTime;
var dd:word; ddd:tdatetime;
begin
  ddd:=StartOfAYear(year);
  while dayofweek(ddd) <> dayofweekstart do
    ddd:=incday(ddd);
  ddd:=incday(ddd,(week-1)*7);
  result:=ddd;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
//  showmessage(inttostr(myweekofyear(strtodate('01/12/2006'),5)));
  showmessage(datetostr(mydateforweek(2,2006,5)));
end;
0
 

Author Comment

by:henryreynolds
ID: 16896251
Hi ciuly

I just want to thank you again, ITS GREAT thanx, thanx,
its' perfect, its exactly what I needed..

Thank you, have a good day

Henry
South Africa
0
 
LVL 12

Accepted Solution

by:
AmigoJack earned 500 total points
ID: 16900291
uhm, i dont want to complain, but this look too simple. weeks dont always start with 1 on the beginning of the year. the years first week is the one which has the thursday in it (ISO). based on the years borders its also a question if 2006-12-31 is week 52, week 53 or even week 1 AND if 2007-01-01 is really week 1 or week 52 or week 53.
0
 

Author Comment

by:henryreynolds
ID: 16909152
Hi sorry I MAD A MISTAKE, I ACCEPTED on the wrong experts name,  ciuly MUST GET ALL THE POINTS
how do a reverse my accept to select ciuly .


Thanx please change it to ciuly


0
 
LVL 28

Expert Comment

by:2266180
ID: 16909291
:) just make a request (open a question) in community support asking for this
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question