在x个月的月份中获得同月的第n天

问题描述 投票:0回答:2

我需要一些帮助,将功能从Access vba转换为vb.net。该脚本根据输入的日期和要添加的月份数生成一个新日期。“如果今天是三月的第二个星期二,那么四个月后的第二个星期二是什么?”

Public Function NdNwk(dType As String, _
        dtSpan As Integer, sDate As Date) As Variant
'  This Function RETURNS the DAY of WHICH WEEK
'  (e.g. Second Tuesday of the Month).
'  FUNCTIONS to be passed to Variables:
'  gtDoW: Day of the WEEK of the START DATE.
'  (1 for Sunday, 2 for Monday, etc.)
'  gtWoM: WEEK of the MONTH of the START DATE.
'  (1 for First, 2 for Second, etc.)
'  gtDSTdt: Desired DATE
'  (generated by the [DateAdd] Function).

'  CALL EXAMPLE: If TODAY is Tuesday, March 10, 2020, 
‘  (second Tuesday of March), then using
'  NdNwk(m, 2, #5/21/2020#)
'  Would generate the DATE 5/12/2020,
'  As the SECOND TUESDAY of MAY.

    Dim gtDSTdt As Date, gtWoM As Integer, gtDoW As Integer
    Dim iLoop As Integer, iPick As Integer, dstDTdom As Date

    gtDoW = Weekday(sDate)
    gtWoM = (Int((Day(sDate) - 1) / 7) + 1)
    gtDSTdt = DateAdd(dType, dtSpan, sDate)

    For iLoop = 1 To Day(DateSerial(Year(gtDSTdt), _
                    Month(gtDSTdt) + 1, 0))

        dstDTdom = DateSerial(Year(gtDSTdt), _
                Month(gtDSTdt), iLoop)

        If Weekday(dstDTdom, 1) = gtDoW Then
            iPick = iPick + 1
            If iPick = gtWoM Then
                NdNwk = dstDTdom
                Exit Function
            End If

        End If
    Next
End Function

在此感谢所有帮助。

vb.net vb.net-2010
2个回答
0
投票

我使用了.net DateTime结构的几种属性和方法。 https://docs.microsoft.com/en-us/dotnet/api/system.datetime?view=netcore-3.1

函数中的算术使用Mod运算符,该运算符返回除法的余数。整数除法(formard斜杠\)返回除法的整数部分。

唯一可能不熟悉的是插值字符串,即以$“”开头的字符串。这使您可以直接将变量嵌入在{ }包围的字符串中。

Private Function NdNwk(InputDate As Date, MonthsAhead As Integer) As String
    Dim newDate As Date
    Dim DofWeek = InputDate.DayOfWeek
    Dim Day = InputDate.Day
    Dim OfInputMonth As Integer
    If Day Mod 7 = 0 Then
        OfInputMonth = Day \ 7
    Else
        OfInputMonth = (Day \ 7) + 1
    End If
    Dim TempDate = InputDate.AddMonths(MonthsAhead)
    Dim NewMonth = TempDate.Month
    Dim NewYear = TempDate.Year
    Dim FirstWeek As Date
    Dim NewDay As Integer
    For d = 1 To 7
        FirstWeek = New Date(TempDate.Year, TempDate.Month, d)
        If FirstWeek.DayOfWeek = DofWeek Then
            NewDay = d
            Exit For
        End If
    Next
    Dim DaysToAdd = (OfInputMonth - 1) * 7
    newDate = New Date(NewYear, NewMonth, NewDay).AddDays(DaysToAdd)
    Dim NewDateString = $"{newDate.ToString("MM/dd/yyyy")} is the {GetOrdinalString(OfInputMonth)} {DofWeek} of {TempDate.ToString("MMMM")}, {TempDate.Year}"
    Return NewDateString
End Function

Private Function GetOrdinalString(input As Integer) As String
    Dim output As String
    Select Case input
        Case 1
            output = "1St"
        Case 2
            output = "2nd"
        Case 3
            output = "3rd"
        Case 4
            output = "4th"
        Case 5
            output = "5th"
        Case Else
            output = ""
    End Select
    Return output
End Function

用法...

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim InputDate As Date
    Dim MonthsToAdd As Integer
    If Not Date.TryParse(TextBox1.Text, InputDate) Then
        MessageBox.Show("Please enter a valid date in Date")
        Return
    End If
    If Not Integer.TryParse(TextBox2.Text, MonthsToAdd) Then
        MessageBox.Show("Please enter a valid number in Months To Add")
        Return
    End If

    Dim d = NdNwk(InputDate, MonthsToAdd)
    MessageBox.Show(d)
End Sub

0
投票

首先,感谢您的所有反馈。我能够一起解析的解决方案如下:

  • 显示月份数的文本框。
  • 显示新日期的文本框。
  • 单击按钮即可运行以下代码:
    Private Sub BtnMonth_Click(sender As Object, e As EventArgs) Handles BtnMonth.Click
        Dim WrkDt As Date = Now


        Dim QtyMnths As Integer = CType(TxtMntCount.Text, Int32)
        Dim newFoM = New Date(WrkDt.Year, WrkDt.Month, 1).AddMonths(QtyMnths)
        Dim DoWDt As Integer = WrkDt.DayOfWeek
        Dim newMntdate = newFoM.AddDays(Enumerable.Range(0,
                         Date.DaysInMonth(newFoM.Year, newFoM.Month) - 1).Where(Function(i) newFoM.AddDays(i).DayOfWeek = DoWDt).Skip(1).First())
        TxtNewDate.Text = Format(newMntdate, "MMMM dd, yyyy (ddd)")
    End Sub

这对我来说很好用!

Mary的解决方案看起来很棒,将来我需要模块化输入时会尝试一下。

再次感谢您的帮助!

© www.soinside.com 2019 - 2024. All rights reserved.