VB.NET根据年月和选择的第几周,得到次周日起范围

前端之家收集整理的这篇文章主要介绍了VB.NET根据年月和选择的第几周,得到次周日起范围前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
    Private Sub Button7_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button7.Click
        MsgBox("范围:" & GetWeekPeriod(Me.monthid.Text,Me.weeks.Text)(0) & " " & GetWeekPeriod(Me.monthid.Text,Me.weeks.Text)(1))
    End Sub

    Private Function GetWeekPeriod(ByVal monthid As String,ByVal curweeks As Integer) As Date()
        Dim totalweeks As Integer
        Dim y,m As String
        y = monthid.Substring(0,4)
        m = monthid.Substring(4)

        Dim FirstDayOfMonth As Date = New DateTime(y,m,1)
        Dim LastDayOfMonth As Date = New DateTime(y,Date.DaysInMonth(y,m))
        Dim d As Date = FirstDayOfMonth
        Dim edweek(1) As Date

        Dim ed(0) As Date

        Dim j As Int16
        For i As Int16 = 0 To Date.DaysInMonth(y,m) - 1
            If d.DayOfWeek = DayOfWeek.Saturday Then
                ReDim Preserve ed(j)
                ed(j) = d
                j = j + 1

                If j = curweeks Then
                    If d.AddDays(-6) < FirstDayOfMonth Then
                        edweek(0) = FirstDayOfMonth
                    Else
                        edweek(0) = d.AddDays(-6)
                    End If
                    edweek(1) = d
                    Return edweek
                End If
            End If
            d = d.AddDays(1)
        Next

        totalweeks = ed.Length
        If ed(ed.Length - 1) <= LastDayOfMonth Then
            totalweeks = totalweeks + 1
        End If

        If curweeks >= totalweeks Then
            edweek(0) = ed(ed.Length - 1).AddDays(1)
            edweek(1) = LastDayOfMonth
        End If

        Return edweek
    End Function
VB.NET根据年月和选择的第几周,得到次周日起范围,这里周的定义是从周日~周六。 原文链接:https://www.f2er.com/vb/260292.html

猜你在找的VB相关文章