Vb.net 如何从一个月的某一天开始计算日期?
我想知道你是否能帮我解决我的约会问题 我有一个名为txtDay的文本框,您可以在其中输入与您发送的消息日期相关的月份日期。这通常是当前日期,因此2015年2月1日将为“01”,该日期将加上当前月份和年份 但是,在某些情况下,您需要输入过去不超过3天的日期。如果是在同一个月,这不是一个问题,但如果是在上个月底,则认为是本月底 例如,在我输入“30”的月份的第一天,这是上个月的第30天,但这只适用于过去不超过3天的情况,其他任何一天都需要在未来,这是可以的 然后从另一个名为txtSTD的文本框中添加时间。然后,这一切都将传递给dtmSTD 我现在的代码是Vb.net 如何从一个月的某一天开始计算日期?,vb.net,date,time,Vb.net,Date,Time,我想知道你是否能帮我解决我的约会问题 我有一个名为txtDay的文本框,您可以在其中输入与您发送的消息日期相关的月份日期。这通常是当前日期,因此2015年2月1日将为“01”,该日期将加上当前月份和年份 但是,在某些情况下,您需要输入过去不超过3天的日期。如果是在同一个月,这不是一个问题,但如果是在上个月底,则认为是本月底 例如,在我输入“30”的月份的第一天,这是上个月的第30天,但这只适用于过去不超过3天的情况,其他任何一天都需要在未来,这是可以的 然后从另一个名为txtSTD的文本框中添加
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
有什么好主意吗
问候,
Andrew我已经根据自己的需要创建了一个函数,下面是其他可能会觉得有用的人的函数 它基本上以字符串形式返回日期,我可以选择返回格式 如果日期超出允许的范围-2/+1天,则返回空字符串
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
这是Visual Basic吗?如果是这样的话,你也许应该这样标记它。它是VB.net,我添加了一个标记。我会避免这种歧义,使用日期时间选择器之类的东西,或者只需要一个有效的日期。用户不太可能喜欢这种神秘的规则。这是必须的,基本上我只需要接受上个月底的一天,但它不是上个月底,而不是本月底。