Week Number

S

Shahid Juma

Hi,

I am trying to figure out a way to get the week number of the current week.
I know this is relatively easy using the calendar year (starting from
January to now). However, I want to do it so I start in October on the first
monday of the month, so today's date would be the 10th week.

Any ideas?

Shahid
 
B

Bob Barrows [MVP]

Shahid said:
Hi,

I am trying to figure out a way to get the week number of the current
week. I know this is relatively easy using the calendar year
(starting from January to now). However, I want to do it so I start
in October on the first monday of the month, so today's date would be
the 10th week.

Any ideas?

Shahid
Create a calendar table that maps each date to the week number you wish to
assign to it.

Bob Barrows
 
E

Evertjan.

Shahid Juma wrote on 09 dec 2004 in
microsoft.public.inetserver.asp.general:
I am trying to figure out a way to get the week number of the current
week. I know this is relatively easy using the calendar year (starting
from January to now). However, I want to do it so I start in October
on the first monday of the month, so today's date would be the 10th
week.

r = datediff("w","2004-05-04","2004-12-15")+1

or perhaps

r = datediff("ww","2004-05-04","2004-12-15")+1
 
D

Dave Anderson

Shahid said:
I am trying to figure out a way to get the week number of the current
week. I know this is relatively easy using the calendar year
(starting from January to now). However, I want to do it so I start
in October on the first monday of the month, so today's date would be
the 10th week.

This is especially difficult when deciding what to do when dealing with
early (pre-Monday) October dates. Take October 1, 2004, for example. Since
your calendar starts on October 4 (the first Monday), you have to move the
start date back to October 6, 2003. Here is a functional JScript
implementation:

Date.prototype.getDoW = function() {
var D1 = new Date(
this.getFullYear(),this.getMonth(),this.getDate()
), D2 = new Date(2000,0,1)
return (
Math.round((D1.valueOf() - D2.valueOf())/86400000)%7 + 6
)%7
}
function weekNum(d) {
var i=0, Day1 = new Date(d.getFullYear(),9)
while (Day1.getDoW() != 1) {
if (Day1 > d) Day1 = new Date(d.getFullYear()-1,9)
Day1.setDate(++i)
}
return Math.ceil((d.valueOf() - Day1.valueOf())/604800000)
}
Response.Write(weekNum(new Date()))


NOTE that Date.getDoW() is merely a zero-based implementation of VBScript's
Weekday Function. Consequently, a VBScript implementation would look
something like this:

Function WeekNumber(D)
Dim Day1
Day1 = DateSerial(Year(D),10,1)
While Weekday(Day1) <> 2
If Day1 > D Then Day1 = DateSerial(Year(D)-1,10,1)
Day1 = DateAdd("d",1,Day1)
Wend
WeekNumber = DateDiff("w",Day1,D) + 1
End Function

Response.Write(WeekNumber(Now))

As you can see, this implementation benefits from VBScript's superior
date-handling functions. It is a straigtforward read, while the JScript
version is not.


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top