日期:2014-05-17  浏览次数:20921 次

一个星期之间的循环
比如今天是2007-1-4号,星期四。
我想让他从星期一开始一直到星期天循环一周。
即:
2007-1-1   星期一
。。。。。
2007-1-7   星期日。
如何做??
前几天他们回的帖子,我试了不是从星期一开始,下面是他给的代码。。。

<%
dim   dateStart//申明当前星期初始日期变量
dateStart   =   date()   -   weekday(date(),   2)//确定当前星期的初始日期
//从初始日期循环7次
for   i   =   0   to   6
response.Write(dateStart+i& ", "&weekday(dateStart+i,   2)& " <br   /> ")
next
%>

------解决方案--------------------
'=======================================//
'SDay : 开始日期
'EDay : 结束日期
'W : 返回星期几
'=======================================//
Function GetWeekStr(SDay,EDay,W)
Dim NDay,Cady
NDay = DateDiff( "d ",SDay,EDay)
Cady = " "
If NDay> =0 Then
For i=0 To NDay
Dim TDay
TDay = DateAdd( "d ",i,SDay)
If Weekday(TDay,2) = W Then CDay = CDay & ", "&TDay
Next
End If
If CDay <> " " Then CDay = Right(CDay,Len(CDay)-1)
GetWeekStr = CDay
End Function
'=======================================//
Response.Write GetWeekStr( "2007-1-8 ", "2007-1-20 ",1)

时间仓促,没怎么测试,你多测几个日期,看有没有BUG
------解决方案--------------------
改进了一下
'=======================================//
Function GetWeekStr(SDay,EDay,W)
Dim NDay,CDay
NDay = DateDiff( "d ",SDay,EDay)
CDay = " "
If NDay> =0 Then
For i=0 To NDay
Dim TDay
TDay = DateAdd( "d ",i,SDay)
If InStr(W,Weekday(TDay,2)) Then CDay = CDay & ", "&TDay
Next
End If
If CDay <> " " Then CDay = Right(CDay,Len(CDay)-1)
GetWeekStr = CDay
End Function
'=======================================//
beginTime = "2007-1-11 "
endTime = "2007-2-17 "
weekstr = "1, 7 "
Response.Write GetWeekStr(beginTime,endTime,weekstr)