100分求一VB函数:返回指定时间段内所有的周、月、季度?
求一VB函数:返回指定时间段内所有的周、月、季度,举个例子:
我指定时间段:2009-01-20-------2010-05-09
现在我传入参数(参数作用:指定返回的是周或月或季度),该函数返回这段时间内所有的季度?
请大家帮忙,谢谢
[解决办法]
你会说,我随便举个例子,对吧?
不过,这在咱们这个行业,叫做需求叙述不清。搞软件的,需要严谨。
[解决办法]
这个差不多吧,试试
Private Function dd(startdt As Date, enddate As Date, dttype As Integer) As Collection
Dim col As Collection
Dim dt As Date
Dim strYear As String
Dim strOther As String
Set col = New Collection
dt = startdt
While dt < enddate
strYear = CStr(Year(dt))
Select Case dttype
Case 1 'week
strOther = Format(Format(dt, "ww"), "00")
dt = DateAdd("ww", 1, dt)
Case 2 'month
strOther = Format(Format(dt, "mm"), "00")
dt = DateAdd("m", 1, dt)
Case Else 'year
strOther = Format(Format(dt, "q"), "00")
dt = DateAdd("q", 1, dt)
End Select
col.Add strYear & strOther
MsgBox strYear & strOther
Wend
Set dd = col
End Function
[解决办法]
Friendly Up
[解决办法]
只要先将起止时间预处理成每个时间段的开始日,然后将循环条件更改为 <= 即可
Private Function dd(ByVal startdt As Date, ByVal enddate As Date, ByVal dttype As Integer) As Collection
Dim col As Collection
Dim dt As Date
Dim strYear As String
Dim strOther As String
Select Case dttype
Case 1 'week
startdt = DateAdd("d", 1 - DatePart("ww", startdt), startdt)
enddate = DateAdd("d", 1 - DatePart("ww", enddate), enddate)
Case 2 'month
startdt = DateSerial(Year(startdt), Month(startdt), 1)
enddate = DateSerial(Year(enddate), Month(enddate), 1)
Case Else 'quarter
startdt = DateSerial(Year(startdt), ((Month(startdt) - 1) \ 3) * 3 + 1, 1)
enddate = DateSerial(Year(enddate), ((Month(enddate) - 1) \ 3) * 3 + 1, 1)
End Select
Set col = New Collection
dt = startdt
While dt <= enddate '<-
strYear = CStr(Year(dt))
Select Case dttype
Case 1 'week
strOther = Format(Format(dt, "ww"), "00")
dt = DateAdd("ww", 1, dt)
Case 2 'month
strOther = Format(Format(dt, "mm"), "00")
dt = DateAdd("m", 1, dt)
Case Else 'quarter
strOther = Format(Format(dt, "q"), "00")
dt = DateAdd("q", 1, dt)
End Select
col.Add strYear & strOther
'MsgBox strYear & strOther
Wend
Set dd = col
End Function