考虑闰年VB.net

时间:2014-02-12 16:39:00

标签: .net vb.net

我有以下一段代码从2月1日开始查看年初至今1月1日。我已经做了更改,但它没有说明闰年。如何修改此代码以确保它占据闰年。我在VB中比较新。有内置功能吗?

Dim dt As Date
    Dim Y2Dday As Integer
    Dim Y2Dyear As Integer
    Dim strDepartment1 As String

    dt = dtpDate.Value
    'Y2Dday = -dt.DayOfYear + 32
    Y2Dday = -dt.DayOfYear + 366
    'If dt.DayOfYear > 31 Then
    If dt.DayOfYear > 365 Then
        Y2Dyear = 0
    Else
        Y2Dyear = -1
    End If
    strDepartment1 = strDepartment.Replace("departmentid", "o.departmentid")

    Dim collection As ItemCollection
    Dim qry As QueryItem

    collection = ItemCollection.GetInstance()
    qry = collection.GetQueryItem(intItem)
    If (Not qry Is Nothing) Then
        qry.SetQueryParameter(QueryItem.QueryParameter.DateFormat, intDateFormat.ToString())

        qry.SetQueryDate(1, dt.ToString)
        'Date used by Item(s) looking at Month to Date
        qry.SetQueryDate(2, dt.AddDays(-dt.Day + 1).ToString)

        'Date used by Item(s) looking at Year to Date
        qry.SetQueryDate(3, dt.AddDays(Y2Dday).AddYears(Y2Dyear).ToString)

        'Date used by Item(s) looking at Yesterday
        qry.SetQueryDate(4, dt.AddDays(-1).ToString)

1 个答案:

答案 0 :(得分:8)

DateTime类型包含IsLeapYear(year as Integer)函数。它是一个共享/静态函数,因此您可以使用带有年份值的DateTime来调用它:

IsLeap = DateTime.IsLeapYear(myYearValue)
'  or
IsLeap = DateTime.IsLeapYear(myDt.Year)

或作为If:

If DateTime.IsLeapYear(myDate.Year) Then ...

无法执行myDateVar.IsLeapYear似乎令人困惑,但它允许您测试年份值而无需先创建DateTime变量。