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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retieve the date of the start of the week from a week number 1

Status
Not open for further replies.

369852

Programmer
May 7, 2002
38
0
0
IE
Hi all,

Would anyone have an easy way of determining the date of the of the start of the week when the user gives the week number?

Thanks in advance
:)
 
If the new year falls in the middle of the week, do you start the week on the previous monday or the next monday?

Next Monday:
Code:
Dim numDays, theDate
numDays = 7 * weekNumber
theDate = DateAdd("d",numDays,cDate("1/1/" & year(Today()))
'if we are not counting first half week:
theDate = DateAdd("d",8 - WeekDay(cDate("1/1/" & year(Today()))),theDate)

'if we are counting from beginning of week before jan 1
theDate = DateAdd("d",-1 * (WeekDay(cDate("1/1/" & Year(Today())) - 1),theDate)

I think that may cover both, I may be a day or so off either way as I just wroe this on the fly, but if you comment out the one you don't need and supply it with the weekNumber variable it should give you the date of the Sunday the week starts on.

-Tarwn ________________________________________________________________________________
Sometimes it is how you ask the question: faq333-2924
Many ASP questions have already been answered, please check faq333-3048 and use the search tool before posting
 
Thanks for the reply!!

just wondering are you missing a bracket here somewhere? I'm not sure where it should go.....

'if we are counting from beginning of week before jan 1
theDate = DateAdd("d",-1 * (WeekDay(cDate("1/1/" & Year(Today())) - 1),theDate)

 
Hi

I chnged your code slightly :( because Today() was giving me trouble.

'if we are counting from beginning of week before jan 1
theDate = DateAdd("d",-1 * (WeekDay(cDate("1/1/2003") - 1)),theDate)

The date i get back is 27/12/1899 which is'nt correct :-(

Anymore ideas for me??
 
Ok, I also just noticed one more problem. We need to subtract 1 from the weeknumber before multiplying by 7, because the first week is 1 - 6 not 8 - 13

With the two missing parans added and the year pulled out so you can specify it before you run the script, the functions now look like this:
Code:
<%
Dim weekNumber, yr
weekNumber = 3
yr = Year(Now())

Dim numDays, theDate, date1, date2
numDays = 7 * (weekNumber - 1)
theDate = DateAdd(&quot;d&quot;,numDays,cDate(&quot;1/1/&quot; & yr))

'if we are not counting first half week:
date1 = DateAdd(&quot;d&quot;,8 - WeekDay(cDate(&quot;1/1/&quot; & yr)),theDate)

'if we are counting from beginning of week before jan 1
date2 = DateAdd(&quot;d&quot;,-1 * (WeekDay(cDate(&quot;1/1/&quot; & yr)) - 1),theDate)

Response.Write &quot;The date of the first day of week &quot; & weekNumber & &quot; is: <Br>&quot;
Response.Write &quot;If not counting first half week of year: &quot; & date1 & &quot;<br>&quot;
Response.Write &quot;If counting first half week of year as full week: &quot; & date2
%>
oh, sorry about the Today() thing, to many languages the last few days :)

Hope that clears up any questions,
-Tarwn ________________________________________________________________________________
Sometimes it is how you ask the question: faq333-2924
Many ASP questions have already been answered, please check faq333-3048 and use the search tool before posting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top