Hello,
I'm trying to determine if a date falls within a range. For instance..
given a range of "Mon 8AM to 5PM".. does "Mon 10AM" fall in that
range? I'm writing my own parser for the dates (date.Parse("Mon 8AM")
only works if now() is a Monday.. or whatever day given in Parse())
but I'm wondering if there is an easy way with something built into
.NET. A few examples are:
"Mon-Fri 8AM to 5PM"
"Mon,Wed,Fri 10PM to 11:30PM"
"1AM to 8AM"
Is there a simple way to do these checks?
Thanks,
Geoffeg
William Stacey [MVP] - 29 Mar 2005 21:09 GMT
Once you have two DateTimes and the comparand, the test is pretty simple.
Does this help?
public static bool DateBetween(DateTime start, DateTime end, DateTime date)
{
if ( date >= start && date <= end )
return true;
return false;
}

Signature
William Stacey, MVP
http://mvp.support.microsoft.com
> Hello,
>
[quoted text clipped - 13 lines]
> Thanks,
> Geoffeg
Jay B. Harlow [MVP - Outlook] - 29 Mar 2005 23:36 GMT
Geoffrey,
I defined a TimeRange type that is useful to check to see if a DateTime
falls within a certain Time Range (for example between 8AM & 5PM). See:
http://groups-beta.google.com/group/microsoft.public.dotnet.languages.vb/msg/809
aa87555043055
It would not be that hard to utilize this type in a larger type that
included one or more Days of Week. Something Like:
Public Structure DayRange
Private ReadOnly m_time As TimeRange
Private ReadOnly m_days() As DayOfWeek
Public Sub New(ByVal time As TimeRange, ByVal ParamArray days() As
DayOfWeek)
m_time = time
m_days = days
End Sub
Public Function Contains(ByVal value As DateTime) As Boolean
Dim index As Integer = Array.IndexOf(m_days, value.DayOfWeek)
Return index <> -1 AndAlso m_time.Contains(value)
End Function
End Structure
Dim range As New DayRange(New TimeRange(#8:00:00 AM#, #5:00:00 PM#),
DayOfWeek.Monday)
Dim value As DateTime
value = #3/29/2005 10:00:00 AM#
Debug.WriteLine(range.Contains(value), value.ToString())
value = #3/28/2005 10:00:00 AM#
Debug.WriteLine(range.Contains(value), value.ToString())
The above DayRange assumes you want to check a specific Date & Time value,
such as DateTime.Now, it should be easy to modify to check a Day of week &
Time value.
Hope this helps
Jay
> Hello,
>
[quoted text clipped - 13 lines]
> Thanks,
> Geoffeg