【问题标题】:VBNet Date CompareVB 净日期比较
【发布时间】:2012-02-03 09:03:43
【问题描述】:

目前,我有这个代码。

Dim iCurrentDate As Date = Now
Dim iStartDate As Date = CDate(dtFrom.Text)
Dim iEndDate As Date = CDate(dtTo.Text)

If (iCurrentDate > iStartDate) And (iCurrentDate < iEndDate) Then
    Console.WriteLine("Current date is within the range.")
    'Other Codes here
Else
    Console.WriteLine("Current date is in the range.")
    'Other Codes here
End If

Dim iObj As Object = IIf((iCurrentDate > iStartDate) And (iCurrentDate < iEndDate), "Current date is within the range.", "Current date is in the range.")
Console.WriteLine(iObj.ToString)
'Other Codes here

上面的代码有什么替代品吗?就像 sql 查询中的 BETWEEN 一样?

【问题讨论】:

    标签: vb.net date compare


    【解决方案1】:

    不,.NET 中没有 BETWEEN 运算符,你真的错过了吗?

    注意

    你可以/应该use IF instead of IIF

    Dim message = _
        If((iCurrentDate > iStartDate) AndAlso (iCurrentDate < iEndDate), _
        "Current date is within the range.", _
        "Current date is not in the range.")
    
    • 更快:只有在第一个是 false 时才会评估第二部分,没有装箱/拆箱
    • 更安全:没有 NullReferenceException,因为您忘记了上面的内容
    • 它更具可读性和类型安全性

    【讨论】:

    【解决方案2】:

    没有内置任何东西,但你可以有一个扩展方法:

    <Extension()> _
    Public Function IsBetween(theDate As DateTime, minDate As DateTime, maxDate As DateTime) As Boolean
        Return theDate > minDate AndAlso theDate < maxDate
    End Function
    

    那么你可以这样使用它:

    If iCurrentDate.IsBetween(iStartDate, iEndDate) Then
        Console.WriteLine("Current date is within the range.")
        'Other Codes here
    Else
        Console.WriteLine("Current date is not in the range.")
        'Other Codes here
    End If
    

    或者像这样:

    Console.WriteLine(If(iCurrentDate.IsBetween(iStartDate, iEndDate), _
                         "Current date is within the range.", _
                         "Current date is not in the range."))
    

    【讨论】:

      【解决方案3】:

      虽然没有直接的替代品,但我在选择案例中看到了这样的东西:

      Select Case iCurrentDate
          Case iStartDate To iEndDate
              'is between
          Case Else
              'is not
      End Select
      

      不过,我从来没有使用过这样的东西。我不确定它的起源是什么。我通常会使用 AndAlso 对您的代码稍作修改:

         If (iCurrentDate > iStartDate) AndAlso (iCurrentDate < iEndDate) Then
                  Console.WriteLine("Current date is within the range.")
                  'Other Codes here
              Else
                  Console.WriteLine("Current date is in the range.")
                  'Other Codes here
              End If
      

      【讨论】:

      • 有趣,我不知道您可以在这样的 Select Case 中比较日期。需要注意的一件事是它可能同时包含 iStartDate 和 iEndDate,因此它相当于:If iCurrentDate &gt;= iStartDate AndAlso iCurrentDate &lt;= iEndDate
      • 是的,我从未测试过。我不确定使用它是好是坏,我从来没有找到任何关于它的文档。
      • 我想这不是一个坏习惯,但它的使用是有限的,因为你不能从范围中排除开始和/或结束日期。通常在我的日期比较中,我会包括开始日期,但不包括结束日期,所以我不能直接使用 Select Case。
      猜你喜欢
      • 2011-12-31
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 2020-09-08
      • 2012-09-06
      • 2015-12-29
      相关资源
      最近更新 更多