我正在尝试使用VBScript查找给定月份内的所有星期四。
有人可以帮忙吗?
答案 0 :(得分:5)
这是一种方式;
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
答案 1 :(得分:0)
虽然accepted answer完成了这项工作,但对于可以使用WeekDay()
函数和For
循环完成的事情来说,它过于复杂。
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
输出:
04/08/2011 = Thursday
11/08/2011 = Thursday
18/08/2011 = Thursday
25/08/2011 = Thursday
此处可以使用Date and Time constant来查找不同的工作日或多个工作日,并进行一些修补。