上周五到下周四

时间:2014-02-27 18:19:47

标签: vbscript asp-classic date-arithmetic

我创建了一个代码来获取上周五,但只有今天已经是下周才会有效。

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

我需要一个代码来根据今天的日期获取最后一个星期五,并获得下一个星期四。

提前致谢。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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