【问题标题】:Validating Date on Date Time Now现在在日期时间验证日期
【发布时间】:2014-12-19 18:46:46
【问题描述】:

嗨,我正在尝试通过 asp 的文本框控件来验证我的身份。

我希望它验证它是否为空,一个日期是今天还是将来而不是过去,并且日期格式是正确的 MM/DD/YYY。

我为此编写了这段代码:

    If String.IsNullOrEmpty(txtPickupDate.Text) Then
        lblError.Text = lblError.Text & "<BR>You must enter in the field Pickup Date."
        lblError.Visible = True
        rtn = False
    Else

        If txtPickupDate.Text > DateTime.Now.ToString("MM/DD/YYYY") Then
            lblError.Text = lblError.Text & "<BR>You must enter in a valid date for Pickup."
            lblError.Visible = True
            rtn = False
        Else
            Dim Pickup As Date
            If Date.TryParseExact(txtPickupDate.Text.ToString(), "mm/dd/yyyy", _
                                  System.Globalization.CultureInfo.CurrentCulture, _
                                  Globalization.DateTimeStyles.None, Pickup) Then
            Else
                lblError.Text = lblError.Text & "<BR>You must enter the Pickup Date in the format of MM/DD/YYYY."
                lblError.Visible = True
                rtn = False
            End If
        End If
    End If

但它在

上不起作用

如果 txtPickupDate.Text > DateTime.Now.ToString("MM/DD/YYYY") 那么

【问题讨论】:

  • 不要对日期数据进行文本比较。 dotnetfiddle.net/czRPsJ
  • 为什么不解析日期字符串?
  • @Plutonix 除非它是 ISO 8601 (en.wikipedia.org/wiki/ISO_8601) 格式并且日期在 [1000 和 10000) 之间。
  • txtPickupDate.Text 解析为一个日期并比较它(日期至今)
  • 我试过 If DateTime.Parse(txtPickupDate.Text) > DateTime.Now Then 还是不行

标签: asp.net vb.net


【解决方案1】:

试试这个。 它将验证是否输入了可解析的日期,然后仅检查日期部分以确定它是过去、现在还是将来。

Dim enteredDate As DateTime
If Not DateTime.TryParse(txtPickupDate.Text, enteredDate) Then
    ' Invalid date entered.
Else
    ' Valid date entered.
    ' Validate against the date part only.
    If enteredDate.Date < DateTime.Today Then
        ' Date is in the past.
    Else
        ' Date is today or in the future.
    End If
End If

【讨论】:

  • 谢谢我终于修好了
【解决方案2】:

我最终这样做了:

    Dim Today As String = DateTime.Today.ToString("MM/dd/yyyy")

    'Due Date 
    If String.IsNullOrEmpty(txtPickupDate.Text) Then
        lblError.Text = lblError.Text & "<BR>You must enter in the field Pickup Date."
        lblError.Visible = True
        rtn = False
    Else

        If txtPickupDate.Text < Today Then
            lblError.Text = lblError.Text & "<BR>You must enter in a valid date for Pickup Date."
            lblError.Visible = True
            rtn = False
        Else
            Dim Pickup As Date
            If Date.TryParseExact(txtPickupDate.Text.ToString(), "MM/dd/yyyy", _
                                  System.Globalization.CultureInfo.CurrentCulture, _
                                  Globalization.DateTimeStyles.None, Pickup) Then
            Else
                lblError.Text = lblError.Text & "<BR>You must enter the Pickup Date in the format of MM/DD/YYYY."
                lblError.Visible = True
                rtn = False
            End If
        End If
    End If

【讨论】:

  • 不要使用字符串来比较数据数据。与 #12/18/2014# 相比,txtPickupDate.Text &lt; Today 将在“2015 年 1 月 31 日”Pickupdate 中失败。
猜你喜欢
  • 2021-05-11
  • 1970-01-01
  • 2015-07-18
  • 1970-01-01
  • 2021-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-05
相关资源
最近更新 更多