7,763
社区成员
发帖
与我相关
我的任务
分享
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
Dim m As String, t As Integer
Set col = New Collection
Select Case dttype
Case 1 'week
m = "ww"
t = DateDiff("ww", startdt, enddate)
Case 2
m = "mm"
t = DateDiff("mm", startdt, enddate)
Case Else
m = "q"
t = DateDiff("q", startdt, enddate)
End Select
dt = startdt
For i = 0 To t
strYear = CStr(Year(dt))
strOther = Format(Format(dt, m), "00")
dt = DateAdd(m, 1, dt)
col.Add strYear & strOther
MsgBox strYear & strOther
Next
Set dd = col
End Function
Function GetDates(dt1 As Date, dt2 As Date, dtInterval As Integer) As String()
'dtInterval: 0=周;1=月;2=季
Dim n As Long, m As Long, k As Long
Dim dt As Date
Dim arrInterval
arrInterval = Array("ww", "m", "q")
n = DateDiff(arrInterval(dtInterval), dt1, dt2)
ReDim Result(n) As String
n = 0
For dt = dt1 To dt2
k = DatePart(arrInterval(dtInterval), dt)
k = k + (k = 53)
If k <> m Then
m = k
Result(n) = Year(dt) & Format(k, "00")
n = n + 1
End If
Next
GetDates = Result
End Function
Private Sub Command1_Click()
Debug.Print Join(GetDates("2009-11-1", Date, 0))
Debug.Print Join(GetDates("2009-11-1", Date, 1))
Debug.Print Join(GetDates("2009-11-1", Date, 2))
End Sub
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