ASP Classic - Find last weeks Friday or any day using days as numbers (1-7, 1=Monday and so on..)

六月ゝ 毕业季﹏ 提交于 2019-12-24 12:06:06

问题


I would like to find last weeks Friday for example. Using days as numbers (1 through 7) for example:

1= Monday and so on..

It would be something like this but I'm stuck at the GetLastWeek, Please see below, THANKS.

<%
dim weeknum
weeknum=5

dim GetLastWeek
GetLastWeek=???? <== FIND LAST WEEKS FRIDAY AS A DATE Eg: MM/DD/YYYY
%>

Example: Last weeks Friday was on: <%=GetLastWeek%>

回答1:


I probably start by working out what is the current day of the week and working back from there, you can use something like this;

Dim today, offsetdays, lastfri
'WeekDay() returns 1 - 7 (Sunday - Saturday).
today = WeekDay(Date())

'Workout the offset then use DateAdd() to minus that number of days.
Select Case today
Case 1 'Sunday
  offsetdays = 2
Case 2 'Monday
  offsetdays = 3
Case 3 'Tuesday
  offsetdays = 4
Case 4 'Wednesday
  offsetdays = 5
Case 5 'Thursday
  offsetdays = 6
Case 6 'Friday
  offsetdays = 7
Case 7 'Saturday
  offsetdays = 1
End Select

lastfri = DateAdd("d", -offsetdays, Date())

Bear in mind this is pseudo coded (untested) and could probably be made better by storing the offsets in an array and using that to power the DateAdd() instead.




回答2:


You can use the Weekday() function to find what day of the week any particular date is. With this you should be able to calculate anything else you like. There is a full reference for the function here:

http://www.w3schools.com/vbscript/func_weekday.asp



来源:https://stackoverflow.com/questions/24499453/asp-classic-find-last-weeks-friday-or-any-day-using-days-as-numbers-1-7-1-mo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!