我想获得当月的星期五日期 这就是我的想法:
functionFindFriday()
if 1st friday then functionFindFriday = '6 dec 2013'
if 2nd friday then functionFindFriday = '13 dec 2013'
if 3rd friday then functionFindFriday = '20 dec 2013'
if 4th friday then functionFindFriday = '20 dec 2013'
end function
我希望用
来调用它 functionFindFriday(1)
它会显示'2013年12月6日'
functionFindFriday(2)
它将显示'2013年12月13日'
答案 0 :(得分:4)
这有效:
Function FindFriday(nFriday As Integer) As Date
Dim dateFirst As Date
Dim dateNthFriday As Date
dateFirst = DateSerial(Year(Now), Month(Now), 1)
dateNthFriday = DateAdd("d", 7 * nFriday - Weekday(dateFirst, vbSaturday), dateFirst)
If DateSerial(Year(dateNthFriday), Month(dateNthFriday), 1) <> dateFirst Then _
Err.Raise 9999, , "There aren't " & nFriday & " Fridays in this month."
FindFriday = dateNthFriday
End Function
答案 1 :(得分:2)
在functionFindFriday()
,
Weekday(that_day)
了解星期几。DateAdd()
至that_day
将that_day
与星期五分开的天数(如果that_day
为vbTuesday
,则添加3)。DateAdd
开始7到上述结果,每次都有另一个星期五,直到下一个结果属于下个月。