2014-02-27 85 views
0

我創建了一個代碼來獲取上一個星期五,但它只有在今天已經是下週時纔有效。獲取上週五並獲取下一個星期四

 iWeekday = Weekday(Now(), vbFriday) 
     LastFridayDate = Now - (iWeekday - 1) 

我需要一個代碼來獲取基於今天日期的最後一個星期五,並獲得下一個星期四。

在此先感謝。

回答

3

你的意思是在星期五你想要上週的星期五?爲此,您只需在週六基於iWeekday

LastFridayDate = Date - Weekday(Date, vbSaturday) 

爲了計算下週四就需要8減去基於週四平日數量,並添加到當前日期:

NextThursdayDate = Date + 8 - Weekday(Date, vbThursday) 

示範:

>>>For i = 0 to 8 : _ d1 = Date + i : _ d2 = d1 - Weekday(d1, vbSaturday) : _ WScript.Echo WeekdayName(Weekday(d1), True) & ", " & d1 & " -> " _ & Weekdayname(Weekday(d2), True) & ", " & d2 : _ Next 
Do, 27.02.2014 -> Fr, 21.02.2014 
Fr, 28.02.2014 -> Fr, 21.02.2014 
Sa, 01.03.2014 -> Fr, 28.02.2014 
So, 02.03.2014 -> Fr, 28.02.2014 
Mo, 03.03.2014 -> Fr, 28.02.2014 
Di, 04.03.2014 -> Fr, 28.02.2014 
Mi, 05.03.2014 -> Fr, 28.02.2014 
Do, 06.03.2014 -> Fr, 28.02.2014 
Fr, 07.03.2014 -> Fr, 28.02.2014 
>>>For i = 0 to 8 : _ d1 = Date + i : _ d2 = d1 + 8 - Weekday(d1, vbThursday) : _ WScript.Echo WeekdayName(Weekday(d1), True) & ", " & d1 & " -> " _ & Weekdayname(Weekday(d2), True) & ", " & d2 : _ Next 
Do, 27.02.2014 -> Do, 06.03.2014 
Fr, 28.02.2014 -> Do, 06.03.2014 
Sa, 01.03.2014 -> Do, 06.03.2014 
So, 02.03.2014 -> Do, 06.03.2014 
Mo, 03.03.2014 -> Do, 06.03.2014 
Di, 04.03.2014 -> Do, 06.03.2014 
Mi, 05.03.2014 -> Do, 06.03.2014 
Do, 06.03.2014 -> Do, 13.03.2014 
Fr, 07.03.2014 -> Do, 13.03.2014
+0

+1有關徹底的解決方案和解釋。 – Lankymart

1

在這裏呀去。

Dim x1, lFriday, lFri : x1 = 1 
Dim x2, nThursday, nThur : x2 = 1 
Dim c 

Do While lFriday <> 6 
c = DateAdd("d", -x1, Now()):lFriday = Weekday(c): lFri = c : x1 = x1 + 1 
Loop 


Do While nThursday <> 5 
c = DateAdd("d", x2, Now()):nThursday = Weekday(c): nThur = c : x2 = x2 + 1 
Loop 
response.write("Last Friday = " & lFri) 
response.write("Next Thursday = " & nThur) 
'Last Friday = lFri 
'Next Thursday = nThur 
+0

感謝#TechnologRich,但#Ansgar Wiechers代碼更簡單。 – MarceloMadnezz

+1

很高興你的答案! – Rich

相關問題