首页 > 解决方案 > Visual basic:我的日历搜索只搜索一年中的第一个月而不是实际月份

问题描述

我正在尝试制作一个从具有随机日期和年份的 csv 文件中的列表读取的应用程序。然后应用程序应该为该随机文件列表制作一个日历。

例如,如果在写入“Sep, 2005”的文件中有一个随机的月份和年份,则应用程序是 2005 年 9 月应该显示月份和年份的日历。它对文件中的每个随机月份和年份执行此操作。它还应该考虑闰年,但它已经这样做了。

我的应用程序读取文件。但是,它只显示一年中的第一个月,而不是指定的月份。例如,如果我运行应用程序并滚动到 2005 年 9 月,它会给我一个 2005 年 1 月的日历,而不是 2005 年 9 月。

我需要一种方法让应用程序显示指定月份而不是一年中的第一个月。

Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click

    Dim strSplit(1) As String
    Dim strRecord As String
    Dim fsrFile As New StreamReader("MonthData.csv")
    Dim intCalendar(5, 6) As Integer
    Dim intCounter As Integer = 0
    Dim intStart As Integer = 0

    Dim strWeek() As String = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
    Dim intYear As Integer
    Dim strMonth() As String = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}
    Dim intMonth() As Integer = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

    Dim store As Integer
    Dim intFirstDayOfYear As Integer
    Dim intDayCount As Integer

    Do
        strRecord = fsrFile.ReadLine()
        strSplit = Split(strRecord, ",")
        intYear = CInt(strSplit(1))
        intCounter = 0

        Console.WriteLine(strRecord)

        For intIndex = 0 To 11
            If strSplit(0) = (strMonth(intIndex)) Then
                store = intIndex
            End If
        Next

        For intIndex = 0 To store
            intFirstDayOfYear = CInt((Int((intYear - 1901) * (365.25) + 2.0) Mod 7)) 'This calculates the first day of the year and is important in display 'January
        Next

        For intRow = 0 To intCalendar.GetUpperBound(0)
            For intCol = 0 To intCalendar.GetUpperBound(1)
                If intRow = 0 Then
                    If intCol = intFirstDayOfYear Then
                        intCounter = 1
                    End If
                End If

                intCalendar(intRow, intCol) = intCounter

                If intCounter > 0 Then
                    intCounter += 1
                End If

                If intCounter > intMonth(store) Then
                    intCounter = 0
                End If

                If intYear Mod 4 = 0 And intCounter > 29 Then 
                    intCounter = 0
                ElseIf intYear Mod 4 = 1 And intCounter > intMonth(store)Then
                    intCounter = 0
'These if else statements calculates the leap year and helps account for it
                End If

            Next
        Next

        For intIndex = 0 To strWeek.GetUpperBound(0)
            Console.Write("{0,4:#}", strWeek(intIndex))
        Next

        Console.WriteLine()

        For intRow = 0 To intCalendar.GetUpperBound(0)
            For intCol = 0 To intCalendar.GetUpperBound(1)
                Console.Write("{0,4:#}", intCalendar(intRow, intCol))
            Next
            Console.WriteLine()
        Next

    Loop Until fsrFile.EndOfStream
End Sub

这是我复制和粘贴的运行/输出的几个月之一。我发布它以帮助证明我所说的。

周日 周一 周二 周三 周四 周五 周六

                            1

    2 3 4 5 6 7 8

    9 10 11 12 13 14 15

    16 17 18 19 20 21 22

    23 24 25 26 27 28 29

    30

如您所见,日历从错误的日期开始,因为它显示的是 1 月而不是 2005 年 9 月

以下是它读取的月份和年份:

Apr,2054
Aug,2058
Apr,1919
Dec,2027
Jul,2049
Feb,1956
Oct,1953
Jan,1983
May,1977
Nov,2081
Sep,2056
Jan,1996
Nov,2029
Feb,1936
Dec,1995
Sep,2054
Feb,2099
Jul,1930
Mar,1938
Oct,2032
May,2051
May,2092
Dec,2067
Jul,2009
Jun,2039
Dec,2069
Oct,2092
Sep,1905
Feb,2081
Aug,2020
Mar,1991
Mar,2042
May,2085
May,2059
Nov,1929
May,1953
Jun,1938
Feb,2051
Aug,1917
Mar,2054
Jul,1936
Aug,1984
Jun,1914
Jul,1905
Jan,2060
Mar,2062
Oct,1940
Nov,1970
May,1909
Feb,2041
Dec,2068
Jan,2063
Sep,2013
Oct,1999
Nov,1942
Jun,2065
Jan,2062
Mar,1969
Aug,2023
Apr,2048
Nov,2053
Apr,2062
Sep,2038
Sep,1938
Apr,1962
Jun,2007
Aug,2093
Oct,1995
Apr,2002
Jan,1957
Feb,1927
Jun,2052
Jul,1920
Aug,1966
Apr,2093
Jun,1914
Dec,2037
Mar,1926
Jul,2046
Sep,2005

标签: vb.net

解决方案


我真的无法弄清楚你想要的结果是什么。您似乎很关心闰年的计算以及一个月中正确的天数。.Net 已经在框架中包含了很多这样的功能。

我使用您的文件数据来演示如何简化您的代码。

Private Sub FunWithDates()
    Dim lines = File.ReadAllLines("C:\Users\*****\Desktop\MonthYear.csv")
    Dim lst As New List(Of Date)
    For Each line In lines
        Dim splits = line.Split(","c)
        Dim MonthNumber = DateTime.ParseExact(splits(0), "MMM", CultureInfo.CurrentCulture).Month
        Dim d As New Date(CInt(splits(1)), MonthNumber, 1)
        lst.Add(d)
    Next
    lst.Sort()
    For Each d In lst
        'This will print an ordered list of Months Years
        Debug.Print($"{d:MMM yyyy} is a leap year? {Date.IsLeapYear(d.Year)}")
    Next
    'To find the last day of a month
    For Each d In lst
        Dim lastDay = d.AddMonths(1).AddDays(-1)
        Debug.Print($"The last day of {d:MMM yyyy} is {lastDay:MMM dd, yyyy}")
    Next
    'Note that the last day of Feb 1956 is the 29th. Check the results of the first loop
    'and you will see that 1956 is indeed a leap year.
    For Each d In lst
        Debug.Print($"{d:MMM dd, yyyy} is a {d.DayOfWeek}")
    Next
    'Note that when using an interpolated string the format for the date follows the colon
    'and is not enclosed by double quotes.
End Sub

推荐阅读