469,328 Members | 1,316 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

Week Number

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
Jul 22 '05 #1
4 11399
Shahid Juma wrote:
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
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 22 '05 #2
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

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 22 '05 #3
Shahid Juma wrote:
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.
Jul 22 '05 #4
Thank you all for your help! I got it working!

Shahid

"Dave Anderson" <GT**********@spammotel.com> wrote in message
news:eG**************@tk2msftngp13.phx.gbl...
Shahid Juma wrote:
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.

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by androtech | last post: by
5 posts views Thread by Bullschmidt | last post: by
7 posts views Thread by Shuffs | last post: by
9 posts views Thread by Ray | last post: by
2 posts views Thread by Rustan | last post: by
3 posts views Thread by Mal | last post: by
18 posts views Thread by Jen | last post: by
6 posts views Thread by aarklon | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.