如何仅从一个月中的某一天计算日期?

How to Calculate date from only the day of the month?

我想知道你是否可以帮我解决我遇到的约会问题。

我有一个名为 txtDay 的文本框,您可以在其中输入与您发送消息的日期相关的月份中的第几天。这通常是当前日期,因此 01FEB15 将是“01”,日期将添加当前月份和年份。

但是,有时您需要为过去不超过 3 天的日期输入日期。如果在同一个月,这不是问题,但如果是上个月的月底,它认为它是当月的月底。

例如,我在当月 1 日输入“30”,这将是上个月的 30 日,但这只适用于过去不超过 3 天,其他任何一天都需要将来也可以。

然后从另一个名为 txtSTD 的文本框中添加时间。然后将其全部传递给 dtmSTD。

我现在的代码是;

    Dim dtmSTD = DateTime.UtcNow

    If txtSTD.Text = "" Then
        MsgBox("Please enter an STD.", MsgBoxStyle.Critical, Me.Text)
        Return False
    ElseIf txtSTD.TextLength < 4 Then
        MsgBox("Please enter a valid STD.", MsgBoxStyle.Critical, Me.Text)
        Return False
    ElseIf txtSTD.TextLength = 4 Then
        If Not DateTime.TryParseExact(txtDay.Text & txtSTD.Text, "ddHHmm", Nothing, Globalization.DateTimeStyles.None, dtmSTD) Then
            MsgBox("Please enter a valid STD.", MsgBoxStyle.Critical, Me.Text)
            Return False
        End If
    End If

有什么建议吗?

此致,

安德鲁

我已经为我的需要创建了一个函数,它在下面供其他可能觉得有用的人使用。

它基本上 return 将日期作为字符串,我可以选择 return 格式。

如果日期超出允许的范围(-2 / +1 天),那么它 return 是一个空字符串。

Function SetDateFromDay(ByVal strdate As String, ByVal strReturnFormat As String, ByVal strTitle As String) As String
    Dim dteDate As DateTime = Date.UtcNow
    Dim strOutput As String = ""
    strdate = strdate.PadLeft(2, "0")

    Dim strLastMonth As String = dteDate.AddMonths(-1).ToString("MMMyyyy").ToUpper
    Dim strNextMonth As String = dteDate.AddMonths(1).ToString("MMMyyyy").ToUpper

    If strdate = dteDate.ToString("dd") Then
        Return dteDate.ToString(strReturnFormat)
    End If

    If strdate = dteDate.AddDays(-1).ToString("dd") Then
        If strdate > dteDate.ToString("dd") Then
            Dim strNewDate = strdate & strLastMonth
            Date.TryParseExact(strNewDate, "ddMMMyyyy", Nothing, Globalization.DateTimeStyles.none, dteDate)
            Return dteDate.ToString(strReturnFormat)
        Else
            Dim strNewDate = strdate & dteDate.ToString("MMMyyyy")
            Date.TryParseExact(strNewDate, "ddMMMyyyy", Nothing, Globalization.DateTimeStyles.none, dteDate)
            Return dteDate.ToString(strReturnFormat)
        End If
    End If

    If strdate = dteDate.AddDays(-2).ToString("dd") Then
        If strdate > dteDate.ToString("dd") Then
            Dim strNewDate = strdate & strLastMonth
            Date.TryParseExact(strNewDate, "ddMMMyyyy", Nothing, Globalization.DateTimeStyles.none, dteDate)
            Return dteDate.ToString(strReturnFormat)
        Else
            Dim strNewDate = strdate & dteDate.ToString("MMMyyyy")
            Date.TryParseExact(strNewDate, "ddMMMyyyy", Nothing, Globalization.DateTimeStyles.none, dteDate)
            Return dteDate.ToString(strReturnFormat)
        End If
    End If

    If strdate = dteDate.AddDays(1).ToString("dd") Then
        If strdate < dteDate.ToString("dd") Then
            Dim strNewDate = strdate & strNextMonth
            Date.TryParseExact(strNewDate, "ddMMMyyyy", Nothing, Globalization.DateTimeStyles.none, dteDate)
            Return dteDate.ToString(strReturnFormat)
        Else
            Dim strNewDate = strdate & dteDate.ToString("MMMyyyy")
            Date.TryParseExact(strNewDate, "ddMMMyyyy", Nothing, Globalization.DateTimeStyles.none, dteDate)
            Return dteDate.ToString(strReturnFormat)
        End If
    End If

    Return ""
End Function