Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / March 2005

Tip: Looking for answers? Try searching our database.

Date Ranges on relative "dates"

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Geoffrey Gallaway - 29 Mar 2005 21:14 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.