根据开始日期和结束日期生成季度

时间:2018-04-10 15:22:56

标签: vb.net

我正在尝试根据开始日期和结束日期生成包含所有季度的表格。我已经编写了一个函数,但不幸的是没有正确计算季度。例如,我有: - 开始日期:01.06.2018 - 结束日期:01.03.2019 我的函数生成Q2,Q3,Q4,正常且正确地生成Q2,Q3,Q4,Q1。

有人有消化吗?

谢谢!

这是我的功能:

Private Function calculateQuartars(ByVal startDate As Date, ByVal endDate As Date) As Collection
    Dim iDate As Date, i As Integer
    Dim quartal As String
    Dim quartalListe As Collection

    iDate = startDate
    i = 0
    Set quartalListe = New Collection

    If (IsDate(startDate) And IsDate(endDate)) Then
        Do          
            quartal = "Q" & DatePart("q", iDate) & " " & DatePart("yyyy", iDate)
            quartalListe.Add quartal, "Q" & i
            iDate = DateAdd("m", 3, iDate)
            i = i + 1
        Loop While iDate < endDate
    Else
        quartal = ""
        quartalListe.Add quartal, "Q" & i
    End If

    Set calculateQuartars = quartalListe

End Function 

2 个答案:

答案 0 :(得分:1)

问题在于以下行

Loop While iDate < endDate

如果日期相等,您的循环将退出

将其更改为 Loop While iDate <= endDate

答案 1 :(得分:1)

我解决了!

if(IsDate(startDate)和IsDate(endDate))然后
            做
           四分之一=&#34; Q&#34; &安培; DatePart(&#34; q&#34;,iDate)&amp; &#34; &#34; &安培; DatePart(&#34; yyyy&#34;,iDate)
            iDate = DateAdd(&#34; m&#34;,1,iDate)
            如果LastQuartal = quartal那么
            否则
            四分之一=&#34; Q&#34; &安培; DatePart(&#34; q&#34;,iDate)&amp; &#34; &#34; &安培; DatePart(&#34; yyyy&#34;,iDate)
            quartalListe.Add quartal
            LastQuartal = quartal
            结束如果
        循环时iDate&lt; = endDate