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.