vb.net循环通过本月和过去6个月

时间:2012-02-02 11:42:08

标签: vb.net loops

我正在尝试循环这个月直到过去六个月使用vb.net并将月份作为整数和年份并将其传递给我的sql语句。但似乎无法弄清楚..

到目前为止我得到了这个:

        Dim dtNow As DateTime = DateTime.Now
        Dim pastSixMonths As DateTime = dtNow.AddMonths(-6)

        For i As Integer = dtNow.Month To pastSixMonths.Month

        Next

请有人帮忙(请不要linq)

1 个答案:

答案 0 :(得分:4)

你想知道如何循环月份Quantum Leap吗?

Dim result As New List(Of String)
Dim currentDate = Now()
Dim last6Month = Now().AddMonths(-6)
While currentDate >= last6Month
    result.Add(String.Format("{0}-{1}", currentDate.Month, currentDate.Year))
    currentDate = currentDate.AddMonths(-1)
End While

result.Clear()
' or the other way round :
currentDate = Now().AddMonths(-6)
While currentDate <= Now()
    result.Add(String.Format("{0}-{1}", currentDate.Month, currentDate.Year))
    currentDate = currentDate.AddMonths(1)
End While