ClareBear
ClareBear

Reputation: 1523

vbscript asp - Find every Thursday in a given month

I'm trying to find all Thursdays within a given Month using VBScript.

Can anyone help?

Upvotes: 2

Views: 1820

Answers (2)

user692942
user692942

Reputation: 16680

While the accepted answer does the job it's overly complicated for something that can be accomplished with WeekDay() function and a For loop.

Dim day
Dim startdate: startdate = CDate("21 aug 2011")
Dim enddate
'Get first day of month.
startdate = DateSerial(Year(startdate), Month(startdate), 1)
'Get last day of month.
enddate = DateAdd("m", 1, startdate) - 1
For day = startdate To enddate
  If WeekDay(day) = vbThursday Then WScript.Echo day & " = " & WeekDayName(WeekDay(day))
Next

Output:

04/08/2011 = Thursday
11/08/2011 = Thursday
18/08/2011 = Thursday
25/08/2011 = Thursday

Any Date and Time constant can be used here to look for different or multiple weekdays with a little tinkering.

Upvotes: 0

Alex K.
Alex K.

Reputation: 175986

Here is one way;

base_date = cdate("21 aug 2011")

'get 1st thursday;
thurs = dateserial(year(base_date), month(base_date), 1)
if (weekday(thurs) <> 1) then thurs = 5 - weekday(thurs) + thurs

'loop subsequent;
do until month(thurs) <> month(base_date)
    msgbox thurs
    thurs = dateadd("d", 7, thurs)
loop

Upvotes: 5

Related Questions