-1

Currently, i have this code.

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

OR

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

Is there any substitute in the above code? Just like BETWEEN in sql queries?

John Woo
  • 258,903
  • 69
  • 498
  • 492

3 Answers3

6

There is nothing built-in, but you could have an extension method:

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

Then you could use it like this:

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

Or like this:

Console.WriteLine(If(iCurrentDate.IsBetween(iStartDate, iEndDate), _
                     "Current date is within the range.", _
                     "Current date is not in the range."))
Meta-Knight
  • 17,626
  • 1
  • 48
  • 58
2

No, there is no BETWEEN operator in .NET, are you really missing it?

Note:

You could/should 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.")
  • It's faster: second part will only be evaluated if first is false, no boxing/unboxing
  • It's safer: no NullRefernceException because you've forgotten above
  • It's more readable and type-safe
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • sir, mind if i ask this question? What is the difference between AND and ANDALSO? – John Woo Jan 05 '12 at 14:53
  • the question has been answered here: http://stackoverflow.com/questions/302047/what-is-the-difference-between-and-and-andalso-in-vb-net. anyway thank you still. – John Woo Jan 05 '12 at 14:57
1

There is no direct substitute I have seen something this with a select case though:

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

I have never used anything like that though. I am not sure what its origin is. I generally go with a slight modification to your code, using 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
Jay
  • 5,897
  • 1
  • 25
  • 28
  • Interesting, I didn't know you could compare dates in a Select Case like this. One thing to be aware of is that it problably includes both iStartDate and iEndDate, so it's the equivalent of: `If iCurrentDate >= iStartDate AndAlso iCurrentDate <= iEndDate` – Meta-Knight Jan 05 '12 at 15:07
  • Yeah, I never tested it. I am not sure if using it is good or bad practice I have never been able to find any documentation on it. – Jay Jan 05 '12 at 15:09
  • I suppose it's not a bad practice, but it's use is limited because you can't exclude the start and/or end dates from the range. Typically in my date comparisons, I would include the start date, but exclude the end date, so I wouldn't be able to use a Select Case directly. – Meta-Knight Jan 05 '12 at 15:15