Find if date falls between 2 dates??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dfetrow410@hotmail.com

    Find if date falls between 2 dates??

    Anyone have some code that will do this?

    Dave

  • Kerry Moorman

    #2
    RE: Find if date falls between 2 dates??

    Dave,

    Dim date1 As DateTime = "12/1/2005"
    Dim date2 As DateTime = "12/31/2005"
    Dim myDate As DateTime

    myDate = InputBox("Date? ")

    If myDate >= date1 And myDate <= date2 Then
    MsgBox("Between ")
    Else
    MsgBox("Not between")
    End If

    Kerry Moorman


    "dfetrow410@hot mail.com" wrote:
    [color=blue]
    > Anyone have some code that will do this?
    >
    > Dave
    >
    >[/color]

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Find if date falls between 2 dates??

      Dave,

      In my opinion is the easiest way to check that using the Ticks property



      I hope this helps,

      Cor


      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: Find if date falls between 2 dates??

        Dave,
        I would use a Range Pattern:


        Dim december2005 As New Range(Of DateTime)(#12/1/2005#, #12/31/2005#)
        If december2005.Co ntains(DateTime .Now) Then
        ' do something exciting because its December!
        End If


        I have a Range(Of T) defined at (usable in VS 2005):



        I have a TimeRange defined at (usable in VS 2002, 2003 & 2005):



        If you want a "plain" DateRange that is usable in VS 2002, 2003 or 2005, you
        can use something like:

        '
        ' Copyright © 2005, Jay B. Harlow, All Rights Reserved.
        '
        Option Strict On
        Option Explicit On

        Public Structure DateRange

        Private ReadOnly m_start As DateTime
        Private ReadOnly m_end As DateTime

        Public Sub New(ByVal start As DateTime, ByVal [end] As DateTime)
        m_start = start.Date
        m_end = [end].Date
        End Sub

        Public ReadOnly Property Start() As DateTime
        Get
        Return m_start
        End Get
        End Property

        Public ReadOnly Property [End]() As DateTime
        Get
        Return m_end
        End Get
        End Property

        Public ReadOnly Property IsEmpty() As Boolean
        Get
        Return m_start.Compare To(Nothing) = 0 AndAlso
        m_end.CompareTo (Nothing) = 0
        Return m_start.Equals( Nothing) AndAlso m_end.Equals(No thing)
        End Get
        End Property

        Public Function Contains(ByVal value As DateTime) As Boolean
        value = value.Date
        Return m_start.Compare To(value) <= 0 AndAlso value.CompareTo (m_end)
        <= 0
        End Function

        Public Function Contains(ByVal value As DateRange) As Boolean
        Return Me.Contains(val ue.m_start) AndAlso Me.Contains(val ue.m_end)
        End Function

        Public Function Overlaps(ByVal value As DateRange) As Boolean
        Return Me.Contains(val ue) OrElse value.Contains( m_start) OrElse
        value.Contains( m_end)
        End Function

        Public Overrides Function GetHashCode() As Integer
        Return m_start.GetHash Code() Xor m_end.GetHashCo de()
        End Function

        Public Overloads Overrides Function Equals(ByVal obj As Object) As
        Boolean
        If TypeOf obj Is DateRange Then
        Return Equals(DirectCa st(obj, DateRange))
        Else
        Return False
        End If
        End Function

        Public Overloads Function Equals(ByVal other As DateRange) As Boolean
        Return m_start.Equals( other.m_start) AndAlso
        m_end.Equals(ot her.m_end)
        End Function

        End Structure

        NOTE: Range(Of DateTime) will include both the Date & the Time values of a
        DateTime, the above DateRange only considers the Date part of a DateTime.
        While the above TimeRange only consider the Time part of a DateTime.

        --
        Hope this helps
        Jay [MVP - Outlook]
        ..NET Application Architect, Enthusiast, & Evangelist
        T.S. Bradley - http://www.tsbradley.net


        <dfetrow410@hot mail.com> wrote in message
        news:1134502092 .339396.287160@ o13g2000cwo.goo glegroups.com.. .
        | Anyone have some code that will do this?
        |
        | Dave
        |


        Comment

        • Dennis

          #5
          Re: Find if date falls between 2 dates??

          Just curious but is there something wrong with the code offered by Kerry?
          His seems a lot more direct and shorter to implement. I don't understand the
          reason for using ranges for determining if a date is between two dates.
          --
          Dennis in Houston


          "Jay B. Harlow [MVP - Outlook]" wrote:
          [color=blue]
          > Dave,
          > I would use a Range Pattern:
          > http://www.martinfowler.com/ap2/range.html
          >
          > Dim december2005 As New Range(Of DateTime)(#12/1/2005#, #12/31/2005#)
          > If december2005.Co ntains(DateTime .Now) Then
          > ' do something exciting because its December!
          > End If
          >
          >
          > I have a Range(Of T) defined at (usable in VS 2005):
          > http://www.tsbradley.net/Cookbook/Ge...ericRange.aspx
          >
          >
          > I have a TimeRange defined at (usable in VS 2002, 2003 & 2005):
          > http://www.tsbradley.net/Cookbook/Pa...timeRange.aspx
          >
          >
          > If you want a "plain" DateRange that is usable in VS 2002, 2003 or 2005, you
          > can use something like:
          >
          > '
          > ' Copyright © 2005, Jay B. Harlow, All Rights Reserved.
          > '
          > Option Strict On
          > Option Explicit On
          >
          > Public Structure DateRange
          >
          > Private ReadOnly m_start As DateTime
          > Private ReadOnly m_end As DateTime
          >
          > Public Sub New(ByVal start As DateTime, ByVal [end] As DateTime)
          > m_start = start.Date
          > m_end = [end].Date
          > End Sub
          >
          > Public ReadOnly Property Start() As DateTime
          > Get
          > Return m_start
          > End Get
          > End Property
          >
          > Public ReadOnly Property [End]() As DateTime
          > Get
          > Return m_end
          > End Get
          > End Property
          >
          > Public ReadOnly Property IsEmpty() As Boolean
          > Get
          > Return m_start.Compare To(Nothing) = 0 AndAlso
          > m_end.CompareTo (Nothing) = 0
          > Return m_start.Equals( Nothing) AndAlso m_end.Equals(No thing)
          > End Get
          > End Property
          >
          > Public Function Contains(ByVal value As DateTime) As Boolean
          > value = value.Date
          > Return m_start.Compare To(value) <= 0 AndAlso value.CompareTo (m_end)
          > <= 0
          > End Function
          >
          > Public Function Contains(ByVal value As DateRange) As Boolean
          > Return Me.Contains(val ue.m_start) AndAlso Me.Contains(val ue.m_end)
          > End Function
          >
          > Public Function Overlaps(ByVal value As DateRange) As Boolean
          > Return Me.Contains(val ue) OrElse value.Contains( m_start) OrElse
          > value.Contains( m_end)
          > End Function
          >
          > Public Overrides Function GetHashCode() As Integer
          > Return m_start.GetHash Code() Xor m_end.GetHashCo de()
          > End Function
          >
          > Public Overloads Overrides Function Equals(ByVal obj As Object) As
          > Boolean
          > If TypeOf obj Is DateRange Then
          > Return Equals(DirectCa st(obj, DateRange))
          > Else
          > Return False
          > End If
          > End Function
          >
          > Public Overloads Function Equals(ByVal other As DateRange) As Boolean
          > Return m_start.Equals( other.m_start) AndAlso
          > m_end.Equals(ot her.m_end)
          > End Function
          >
          > End Structure
          >
          > NOTE: Range(Of DateTime) will include both the Date & the Time values of a
          > DateTime, the above DateRange only considers the Date part of a DateTime.
          > While the above TimeRange only consider the Time part of a DateTime.
          >
          > --
          > Hope this helps
          > Jay [MVP - Outlook]
          > ..NET Application Architect, Enthusiast, & Evangelist
          > T.S. Bradley - http://www.tsbradley.net
          >
          >
          > <dfetrow410@hot mail.com> wrote in message
          > news:1134502092 .339396.287160@ o13g2000cwo.goo glegroups.com.. .
          > | Anyone have some code that will do this?
          > |
          > | Dave
          > |
          >
          >
          >[/color]

          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: Find if date falls between 2 dates??

            Dennis,
            | I don't understand the
            | reason for using ranges for determining if a date is between two dates.

            Martin Fowler I believe answers that question at:
            | > http://www.martinfowler.com/ap2/range.html

            Here are my thoughts on it, in a David Letterman style top 10 list ;-)

            10) It has to do with Object Thinking & OO; Abstraction, Encapsulation, Code
            Reuse, Duplication.

            9) With Kerry's code you have two independent variables floating around,
            which are related via the expression. being independent, means that can
            change independently possibly inappropriately .

            8) With Kerry's code you have an expression that is possibly duplicated all
            over your program. Doesn't necessarily leverage code reuse

            7) DateRange & TimeRange *encapsulate* special logic in them to ensure that
            the Date part or the Time part of a DateTime are handled correctly!

            6) With a Range object you have a single variable which has specific
            attributes & behavior (an Abstraction).

            5) The Range object has a Start, End properties and also Contains & Overlaps
            methods or behaviors (Encapsulation) .

            4) With a Range object you are thinking in terms of an Object, the Range
            itself. You ask the range if this value is contained within it.

            3) With a Range object the expression is confined to the Range.Contains
            method. In fact the overloaded Range.Contains method & the Range.Overlaps
            method are implemented in terms of the first Range.Contains method avoiding
            duplication.

            2) With a Range object, you only need to test the Range type "once", then
            you are "assured" that Range will always return the results you want. Code
            reuse, verifiable.

            1) More importantly I consider the usage of the Range object to be more
            readable.

            Given:
            Dim date1 As DateTime = "12/1/2005"
            Dim date2 As DateTime = "12/31/2005"

            Dim december2005 As New Range(Of DateTime)(#12/1/2005#, #12/31/2005#)


            Dim myDate As DateTime = DateTime.Now

            Which of the following more directly shows that you are checking to see if
            December 2005 contains myDate?

            If december2005.Co ntains(myDate) Then

            If myDate >= date1 And myDate <= date2 Then

            Even if you rename date1 & date2 to start & finish, you need to think about
            what the expression means...

            BTW: I used "Contains" rather then "Between" or "Includes" to be consistent
            with the rest of the Framework, so simply seeing "Contains" above should
            give you a clue that
            "december2005.C ontains(myDate) " is doing something similar to
            "list.Contains( key)"...


            Now if you only have a single place where you are checking a Range for a
            Value, then yes a Range object may be overkill, however if you are checking
            a Range for a Value in a number of places, such as time entry, time
            accounting, insurance products, or anything else that deals with a
            "calendar", then I would expect the Range will simplify & clarify your code!


            BTW: If you look closely you will see that the Range.Contains method is
            effectively the same expression as Kerry's code. The Range simply
            encapsulates Kerry's date1 & date2 variables & his expression into a single
            Type.

            If myDate >= date1 And myDate <= date2 Then
            | > Return m_start.Compare To(value) <= 0 AndAlso
            value.CompareTo (m_end)
            | > <= 0

            To be useful this Type then has other behavior added to it (the overloaded
            Contains, the Overlaps, Equality, properties). Also the Range is designed as
            Immutable, meaning you need to create an entirely new Range rather then
            modifying an existing Range.

            When I converted the Range(Of T) to DateRange, I left the
            IComparable.Com pareTo methods in instead of unwrapping them to the
            overloaded operators. The Range(Of T) itself cannot use the overloaded
            operators.

            --
            Hope this helps
            Jay [MVP - Outlook]
            ..NET Application Architect, Enthusiast, & Evangelist
            T.S. Bradley - http://www.tsbradley.net


            "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
            news:C850C553-17D1-472F-8B00-DEAB3125A848@mi crosoft.com...
            | Just curious but is there something wrong with the code offered by Kerry?
            | His seems a lot more direct and shorter to implement. I don't understand
            the
            | reason for using ranges for determining if a date is between two dates.
            | --
            | Dennis in Houston
            |
            |
            | "Jay B. Harlow [MVP - Outlook]" wrote:
            |
            | > Dave,
            | > I would use a Range Pattern:
            | > http://www.martinfowler.com/ap2/range.html
            | >
            | > Dim december2005 As New Range(Of DateTime)(#12/1/2005#,
            #12/31/2005#)
            | > If december2005.Co ntains(DateTime .Now) Then
            | > ' do something exciting because its December!
            | > End If
            | >
            | >
            | > I have a Range(Of T) defined at (usable in VS 2005):
            | > http://www.tsbradley.net/Cookbook/Ge...ericRange.aspx
            | >
            | >
            | > I have a TimeRange defined at (usable in VS 2002, 2003 & 2005):
            | > http://www.tsbradley.net/Cookbook/Pa...timeRange.aspx
            | >
            | >
            | > If you want a "plain" DateRange that is usable in VS 2002, 2003 or 2005,
            you
            | > can use something like:
            | >
            | > '
            | > ' Copyright © 2005, Jay B. Harlow, All Rights Reserved.
            | > '
            | > Option Strict On
            | > Option Explicit On
            | >
            | > Public Structure DateRange
            | >
            | > Private ReadOnly m_start As DateTime
            | > Private ReadOnly m_end As DateTime
            | >
            | > Public Sub New(ByVal start As DateTime, ByVal [end] As DateTime)
            | > m_start = start.Date
            | > m_end = [end].Date
            | > End Sub
            | >
            | > Public ReadOnly Property Start() As DateTime
            | > Get
            | > Return m_start
            | > End Get
            | > End Property
            | >
            | > Public ReadOnly Property [End]() As DateTime
            | > Get
            | > Return m_end
            | > End Get
            | > End Property
            | >
            | > Public ReadOnly Property IsEmpty() As Boolean
            | > Get
            | > Return m_start.Compare To(Nothing) = 0 AndAlso
            | > m_end.CompareTo (Nothing) = 0
            | > Return m_start.Equals( Nothing) AndAlso m_end.Equals(No thing)
            | > End Get
            | > End Property
            | >
            | > Public Function Contains(ByVal value As DateTime) As Boolean
            | > value = value.Date
            | > Return m_start.Compare To(value) <= 0 AndAlso
            value.CompareTo (m_end)
            | > <= 0
            | > End Function
            | >
            | > Public Function Contains(ByVal value As DateRange) As Boolean
            | > Return Me.Contains(val ue.m_start) AndAlso
            Me.Contains(val ue.m_end)
            | > End Function
            | >
            | > Public Function Overlaps(ByVal value As DateRange) As Boolean
            | > Return Me.Contains(val ue) OrElse value.Contains( m_start) OrElse
            | > value.Contains( m_end)
            | > End Function
            | >
            | > Public Overrides Function GetHashCode() As Integer
            | > Return m_start.GetHash Code() Xor m_end.GetHashCo de()
            | > End Function
            | >
            | > Public Overloads Overrides Function Equals(ByVal obj As Object) As
            | > Boolean
            | > If TypeOf obj Is DateRange Then
            | > Return Equals(DirectCa st(obj, DateRange))
            | > Else
            | > Return False
            | > End If
            | > End Function
            | >
            | > Public Overloads Function Equals(ByVal other As DateRange) As
            Boolean
            | > Return m_start.Equals( other.m_start) AndAlso
            | > m_end.Equals(ot her.m_end)
            | > End Function
            | >
            | > End Structure
            | >
            | > NOTE: Range(Of DateTime) will include both the Date & the Time values of
            a
            | > DateTime, the above DateRange only considers the Date part of a
            DateTime.
            | > While the above TimeRange only consider the Time part of a DateTime.
            | >
            | > --
            | > Hope this helps
            | > Jay [MVP - Outlook]
            | > ..NET Application Architect, Enthusiast, & Evangelist
            | > T.S. Bradley - http://www.tsbradley.net
            | >
            | >
            | > <dfetrow410@hot mail.com> wrote in message
            | > news:1134502092 .339396.287160@ o13g2000cwo.goo glegroups.com.. .
            | > | Anyone have some code that will do this?
            | > |
            | > | Dave
            | > |
            | >
            | >
            | >


            Comment

            • Cor Ligthert [MVP]

              #7
              Re: Find if date falls between 2 dates??

              Dennis,

              Jay did strange enough not gave explicit these two.

              The code offered by Kerry does only work with option Strict Off, which means
              a lower performance.

              If your program is used at almost any place outside the USA, than your
              program gives lucky enough an error and breaks and does not a give a wrong
              answer.

              However I keep it for this simple problem as you stated it by my solution.

              Cor


              "Dennis" <Dennis@discuss ions.microsoft. com> schreef in bericht
              news:C850C553-17D1-472F-8B00-DEAB3125A848@mi crosoft.com...[color=blue]
              > Just curious but is there something wrong with the code offered by Kerry?
              > His seems a lot more direct and shorter to implement. I don't understand
              > the
              > reason for using ranges for determining if a date is between two dates.
              > --
              > Dennis in Houston
              >
              >
              > "Jay B. Harlow [MVP - Outlook]" wrote:
              >[color=green]
              >> Dave,
              >> I would use a Range Pattern:
              >> http://www.martinfowler.com/ap2/range.html
              >>
              >> Dim december2005 As New Range(Of DateTime)(#12/1/2005#, #12/31/2005#)
              >> If december2005.Co ntains(DateTime .Now) Then
              >> ' do something exciting because its December!
              >> End If
              >>
              >>
              >> I have a Range(Of T) defined at (usable in VS 2005):
              >> http://www.tsbradley.net/Cookbook/Ge...ericRange.aspx
              >>
              >>
              >> I have a TimeRange defined at (usable in VS 2002, 2003 & 2005):
              >> http://www.tsbradley.net/Cookbook/Pa...timeRange.aspx
              >>
              >>
              >> If you want a "plain" DateRange that is usable in VS 2002, 2003 or 2005,
              >> you
              >> can use something like:
              >>
              >> '
              >> ' Copyright © 2005, Jay B. Harlow, All Rights Reserved.
              >> '
              >> Option Strict On
              >> Option Explicit On
              >>
              >> Public Structure DateRange
              >>
              >> Private ReadOnly m_start As DateTime
              >> Private ReadOnly m_end As DateTime
              >>
              >> Public Sub New(ByVal start As DateTime, ByVal [end] As DateTime)
              >> m_start = start.Date
              >> m_end = [end].Date
              >> End Sub
              >>
              >> Public ReadOnly Property Start() As DateTime
              >> Get
              >> Return m_start
              >> End Get
              >> End Property
              >>
              >> Public ReadOnly Property [End]() As DateTime
              >> Get
              >> Return m_end
              >> End Get
              >> End Property
              >>
              >> Public ReadOnly Property IsEmpty() As Boolean
              >> Get
              >> Return m_start.Compare To(Nothing) = 0 AndAlso
              >> m_end.CompareTo (Nothing) = 0
              >> Return m_start.Equals( Nothing) AndAlso m_end.Equals(No thing)
              >> End Get
              >> End Property
              >>
              >> Public Function Contains(ByVal value As DateTime) As Boolean
              >> value = value.Date
              >> Return m_start.Compare To(value) <= 0 AndAlso
              >> value.CompareTo (m_end)
              >> <= 0
              >> End Function
              >>
              >> Public Function Contains(ByVal value As DateRange) As Boolean
              >> Return Me.Contains(val ue.m_start) AndAlso
              >> Me.Contains(val ue.m_end)
              >> End Function
              >>
              >> Public Function Overlaps(ByVal value As DateRange) As Boolean
              >> Return Me.Contains(val ue) OrElse value.Contains( m_start) OrElse
              >> value.Contains( m_end)
              >> End Function
              >>
              >> Public Overrides Function GetHashCode() As Integer
              >> Return m_start.GetHash Code() Xor m_end.GetHashCo de()
              >> End Function
              >>
              >> Public Overloads Overrides Function Equals(ByVal obj As Object) As
              >> Boolean
              >> If TypeOf obj Is DateRange Then
              >> Return Equals(DirectCa st(obj, DateRange))
              >> Else
              >> Return False
              >> End If
              >> End Function
              >>
              >> Public Overloads Function Equals(ByVal other As DateRange) As Boolean
              >> Return m_start.Equals( other.m_start) AndAlso
              >> m_end.Equals(ot her.m_end)
              >> End Function
              >>
              >> End Structure
              >>
              >> NOTE: Range(Of DateTime) will include both the Date & the Time values of
              >> a
              >> DateTime, the above DateRange only considers the Date part of a DateTime.
              >> While the above TimeRange only consider the Time part of a DateTime.
              >>
              >> --
              >> Hope this helps
              >> Jay [MVP - Outlook]
              >> ..NET Application Architect, Enthusiast, & Evangelist
              >> T.S. Bradley - http://www.tsbradley.net
              >>
              >>
              >> <dfetrow410@hot mail.com> wrote in message
              >> news:1134502092 .339396.287160@ o13g2000cwo.goo glegroups.com.. .
              >> | Anyone have some code that will do this?
              >> |
              >> | Dave
              >> |
              >>
              >>
              >>[/color][/color]


              Comment

              • Dennis

                #8
                Re: Find if date falls between 2 dates??

                Thanks. I believe I like the below for my purposes being a sole programmer
                and not part of a team effort:

                If StartDate.Compa reTo(TestDate) <= 0 AndAlso TestDate.Compar eTo(EndDate) <= 0

                As Jay suggests, using a DateRange Class would be the way to go if comparing
                a lot of dates.

                --
                Dennis in Houston


                "Cor Ligthert [MVP]" wrote:
                [color=blue]
                > Dennis,
                >
                > Jay did strange enough not gave explicit these two.
                >
                > The code offered by Kerry does only work with option Strict Off, which means
                > a lower performance.
                >
                > If your program is used at almost any place outside the USA, than your
                > program gives lucky enough an error and breaks and does not a give a wrong
                > answer.
                >
                > However I keep it for this simple problem as you stated it by my solution.
                >
                > Cor
                >
                >
                > "Dennis" <Dennis@discuss ions.microsoft. com> schreef in bericht
                > news:C850C553-17D1-472F-8B00-DEAB3125A848@mi crosoft.com...[color=green]
                > > Just curious but is there something wrong with the code offered by Kerry?
                > > His seems a lot more direct and shorter to implement. I don't understand
                > > the
                > > reason for using ranges for determining if a date is between two dates.
                > > --
                > > Dennis in Houston
                > >
                > >
                > > "Jay B. Harlow [MVP - Outlook]" wrote:
                > >[color=darkred]
                > >> Dave,
                > >> I would use a Range Pattern:
                > >> http://www.martinfowler.com/ap2/range.html
                > >>
                > >> Dim december2005 As New Range(Of DateTime)(#12/1/2005#, #12/31/2005#)
                > >> If december2005.Co ntains(DateTime .Now) Then
                > >> ' do something exciting because its December!
                > >> End If
                > >>
                > >>
                > >> I have a Range(Of T) defined at (usable in VS 2005):
                > >> http://www.tsbradley.net/Cookbook/Ge...ericRange.aspx
                > >>
                > >>
                > >> I have a TimeRange defined at (usable in VS 2002, 2003 & 2005):
                > >> http://www.tsbradley.net/Cookbook/Pa...timeRange.aspx
                > >>
                > >>
                > >> If you want a "plain" DateRange that is usable in VS 2002, 2003 or 2005,
                > >> you
                > >> can use something like:
                > >>
                > >> '
                > >> ' Copyright © 2005, Jay B. Harlow, All Rights Reserved.
                > >> '
                > >> Option Strict On
                > >> Option Explicit On
                > >>
                > >> Public Structure DateRange
                > >>
                > >> Private ReadOnly m_start As DateTime
                > >> Private ReadOnly m_end As DateTime
                > >>
                > >> Public Sub New(ByVal start As DateTime, ByVal [end] As DateTime)
                > >> m_start = start.Date
                > >> m_end = [end].Date
                > >> End Sub
                > >>
                > >> Public ReadOnly Property Start() As DateTime
                > >> Get
                > >> Return m_start
                > >> End Get
                > >> End Property
                > >>
                > >> Public ReadOnly Property [End]() As DateTime
                > >> Get
                > >> Return m_end
                > >> End Get
                > >> End Property
                > >>
                > >> Public ReadOnly Property IsEmpty() As Boolean
                > >> Get
                > >> Return m_start.Compare To(Nothing) = 0 AndAlso
                > >> m_end.CompareTo (Nothing) = 0
                > >> Return m_start.Equals( Nothing) AndAlso m_end.Equals(No thing)
                > >> End Get
                > >> End Property
                > >>
                > >> Public Function Contains(ByVal value As DateTime) As Boolean
                > >> value = value.Date
                > >> Return m_start.Compare To(value) <= 0 AndAlso
                > >> value.CompareTo (m_end)
                > >> <= 0
                > >> End Function
                > >>
                > >> Public Function Contains(ByVal value As DateRange) As Boolean
                > >> Return Me.Contains(val ue.m_start) AndAlso
                > >> Me.Contains(val ue.m_end)
                > >> End Function
                > >>
                > >> Public Function Overlaps(ByVal value As DateRange) As Boolean
                > >> Return Me.Contains(val ue) OrElse value.Contains( m_start) OrElse
                > >> value.Contains( m_end)
                > >> End Function
                > >>
                > >> Public Overrides Function GetHashCode() As Integer
                > >> Return m_start.GetHash Code() Xor m_end.GetHashCo de()
                > >> End Function
                > >>
                > >> Public Overloads Overrides Function Equals(ByVal obj As Object) As
                > >> Boolean
                > >> If TypeOf obj Is DateRange Then
                > >> Return Equals(DirectCa st(obj, DateRange))
                > >> Else
                > >> Return False
                > >> End If
                > >> End Function
                > >>
                > >> Public Overloads Function Equals(ByVal other As DateRange) As Boolean
                > >> Return m_start.Equals( other.m_start) AndAlso
                > >> m_end.Equals(ot her.m_end)
                > >> End Function
                > >>
                > >> End Structure
                > >>
                > >> NOTE: Range(Of DateTime) will include both the Date & the Time values of
                > >> a
                > >> DateTime, the above DateRange only considers the Date part of a DateTime.
                > >> While the above TimeRange only consider the Time part of a DateTime.
                > >>
                > >> --
                > >> Hope this helps
                > >> Jay [MVP - Outlook]
                > >> ..NET Application Architect, Enthusiast, & Evangelist
                > >> T.S. Bradley - http://www.tsbradley.net
                > >>
                > >>
                > >> <dfetrow410@hot mail.com> wrote in message
                > >> news:1134502092 .339396.287160@ o13g2000cwo.goo glegroups.com.. .
                > >> | Anyone have some code that will do this?
                > >> |
                > >> | Dave
                > >> |
                > >>
                > >>
                > >>[/color][/color]
                >
                >
                >[/color]

                Comment

                • Jay B. Harlow [MVP - Outlook]

                  #9
                  Re: Find if date falls between 2 dates??

                  Cor,
                  | The code offered by Kerry does only work with option Strict Off, which
                  means
                  | a lower performance.
                  ??

                  Kerry's code works for me with Option Strict On in both VB 2003 & VB 2005. I
                  would expect VB 2002 also. As VB has always overloaded the comparison
                  operators for DateTime & Decimal.

                  VB 2002 & 2003 however has never overloaded any operators for TimeSpan.

                  While VB 2005 offers full support for defining & using overloaded operators.
                  So Kerry's code will work with TimeSpans & other types that overload the <=
                  operator in VS 2005.

                  --
                  Hope this helps
                  Jay [MVP - Outlook]
                  ..NET Application Architect, Enthusiast, & Evangelist
                  T.S. Bradley - http://www.tsbradley.net


                  "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
                  news:OIg3N2NAGH A.208@tk2msftng p13.phx.gbl...
                  | Dennis,
                  |
                  | Jay did strange enough not gave explicit these two.
                  |
                  | The code offered by Kerry does only work with option Strict Off, which
                  means
                  | a lower performance.
                  |
                  | If your program is used at almost any place outside the USA, than your
                  | program gives lucky enough an error and breaks and does not a give a wrong
                  | answer.
                  |
                  | However I keep it for this simple problem as you stated it by my solution.
                  |
                  | Cor
                  |
                  |
                  | "Dennis" <Dennis@discuss ions.microsoft. com> schreef in bericht
                  | news:C850C553-17D1-472F-8B00-DEAB3125A848@mi crosoft.com...
                  | > Just curious but is there something wrong with the code offered by
                  Kerry?
                  | > His seems a lot more direct and shorter to implement. I don't
                  understand
                  | > the
                  | > reason for using ranges for determining if a date is between two dates.
                  | > --
                  | > Dennis in Houston
                  | >
                  | >
                  | > "Jay B. Harlow [MVP - Outlook]" wrote:
                  | >
                  | >> Dave,
                  | >> I would use a Range Pattern:
                  | >> http://www.martinfowler.com/ap2/range.html
                  | >>
                  | >> Dim december2005 As New Range(Of DateTime)(#12/1/2005#,
                  #12/31/2005#)
                  | >> If december2005.Co ntains(DateTime .Now) Then
                  | >> ' do something exciting because its December!
                  | >> End If
                  | >>
                  | >>
                  | >> I have a Range(Of T) defined at (usable in VS 2005):
                  | >> http://www.tsbradley.net/Cookbook/Ge...ericRange.aspx
                  | >>
                  | >>
                  | >> I have a TimeRange defined at (usable in VS 2002, 2003 & 2005):
                  | >> http://www.tsbradley.net/Cookbook/Pa...timeRange.aspx
                  | >>
                  | >>
                  | >> If you want a "plain" DateRange that is usable in VS 2002, 2003 or
                  2005,
                  | >> you
                  | >> can use something like:
                  | >>
                  | >> '
                  | >> ' Copyright © 2005, Jay B. Harlow, All Rights Reserved.
                  | >> '
                  | >> Option Strict On
                  | >> Option Explicit On
                  | >>
                  | >> Public Structure DateRange
                  | >>
                  | >> Private ReadOnly m_start As DateTime
                  | >> Private ReadOnly m_end As DateTime
                  | >>
                  | >> Public Sub New(ByVal start As DateTime, ByVal [end] As DateTime)
                  | >> m_start = start.Date
                  | >> m_end = [end].Date
                  | >> End Sub
                  | >>
                  | >> Public ReadOnly Property Start() As DateTime
                  | >> Get
                  | >> Return m_start
                  | >> End Get
                  | >> End Property
                  | >>
                  | >> Public ReadOnly Property [End]() As DateTime
                  | >> Get
                  | >> Return m_end
                  | >> End Get
                  | >> End Property
                  | >>
                  | >> Public ReadOnly Property IsEmpty() As Boolean
                  | >> Get
                  | >> Return m_start.Compare To(Nothing) = 0 AndAlso
                  | >> m_end.CompareTo (Nothing) = 0
                  | >> Return m_start.Equals( Nothing) AndAlso
                  m_end.Equals(No thing)
                  | >> End Get
                  | >> End Property
                  | >>
                  | >> Public Function Contains(ByVal value As DateTime) As Boolean
                  | >> value = value.Date
                  | >> Return m_start.Compare To(value) <= 0 AndAlso
                  | >> value.CompareTo (m_end)
                  | >> <= 0
                  | >> End Function
                  | >>
                  | >> Public Function Contains(ByVal value As DateRange) As Boolean
                  | >> Return Me.Contains(val ue.m_start) AndAlso
                  | >> Me.Contains(val ue.m_end)
                  | >> End Function
                  | >>
                  | >> Public Function Overlaps(ByVal value As DateRange) As Boolean
                  | >> Return Me.Contains(val ue) OrElse value.Contains( m_start) OrElse
                  | >> value.Contains( m_end)
                  | >> End Function
                  | >>
                  | >> Public Overrides Function GetHashCode() As Integer
                  | >> Return m_start.GetHash Code() Xor m_end.GetHashCo de()
                  | >> End Function
                  | >>
                  | >> Public Overloads Overrides Function Equals(ByVal obj As Object) As
                  | >> Boolean
                  | >> If TypeOf obj Is DateRange Then
                  | >> Return Equals(DirectCa st(obj, DateRange))
                  | >> Else
                  | >> Return False
                  | >> End If
                  | >> End Function
                  | >>
                  | >> Public Overloads Function Equals(ByVal other As DateRange) As
                  Boolean
                  | >> Return m_start.Equals( other.m_start) AndAlso
                  | >> m_end.Equals(ot her.m_end)
                  | >> End Function
                  | >>
                  | >> End Structure
                  | >>
                  | >> NOTE: Range(Of DateTime) will include both the Date & the Time values
                  of
                  | >> a
                  | >> DateTime, the above DateRange only considers the Date part of a
                  DateTime.
                  | >> While the above TimeRange only consider the Time part of a DateTime.
                  | >>
                  | >> --
                  | >> Hope this helps
                  | >> Jay [MVP - Outlook]
                  | >> ..NET Application Architect, Enthusiast, & Evangelist
                  | >> T.S. Bradley - http://www.tsbradley.net
                  | >>
                  | >>
                  | >> <dfetrow410@hot mail.com> wrote in message
                  | >> news:1134502092 .339396.287160@ o13g2000cwo.goo glegroups.com.. .
                  | >> | Anyone have some code that will do this?
                  | >> |
                  | >> | Dave
                  | >> |
                  | >>
                  | >>
                  | >>
                  |
                  |


                  Comment

                  • Cor Ligthert [MVP]

                    #10
                    Re: Find if date falls between 2 dates??

                    Jay,
                    [color=blue]
                    > Kerry's code works for me with Option Strict On in both VB 2003 & VB 2005.
                    > I
                    > would expect VB 2002 also. As VB has always overloaded the comparison
                    > operators for DateTime & Decimal.[/color]


                    Are you sure of that, than I should have understand it forever wrong.

                    Kerry's starting code
                    [color=blue][color=green]
                    >>Dim date1 As DateTime = "12/1/2005"[/color][/color]

                    It says by me that Option Strict Disallows direct converstion from String to
                    TimeDate.

                    If you want can I make next time in my messages that it does not work for
                    me, however does for you with Option Strict on in all versions.

                    :-)

                    Cor


                    Comment

                    • Jay B. Harlow [MVP - Outlook]

                      #11
                      Re: Find if date falls between 2 dates??

                      Cor,
                      | > Kerry's code works for me with Option Strict On in both VB 2003 & VB
                      2005.
                      | > I
                      | > would expect VB 2002 also. As VB has always overloaded the comparison
                      | > operators for DateTime & Decimal.
                      | Are you sure of that, than I should have understand it forever wrong.
                      Yes as I explicitly tried the following line of Kerry's code:

                      If myDate >= date1 And myDate <= date2 Then

                      However you are correct, his initialization code is not does not work.

                      Dim date1 As DateTime = "12/1/2005"

                      Which is where I would use
                      Dim date1 As DateTime = #12/1/2005#


                      My point (for Dennis really) is that DateTime.Compar eTo is not required as
                      DateTime <= works. TimeSpan.Compar eTo is required in VB 2002 & 2003, however
                      VB 2005 allows TimeSpan <=.


                      --
                      Hope this helps
                      Jay [MVP - Outlook]
                      ..NET Application Architect, Enthusiast, & Evangelist
                      T.S. Bradley - http://www.tsbradley.net


                      "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
                      news:%23wPWwvgA GHA.3352@TK2MSF TNGP10.phx.gbl. ..
                      | Jay,
                      |
                      | > Kerry's code works for me with Option Strict On in both VB 2003 & VB
                      2005.
                      | > I
                      | > would expect VB 2002 also. As VB has always overloaded the comparison
                      | > operators for DateTime & Decimal.
                      |
                      |
                      | Are you sure of that, than I should have understand it forever wrong.
                      |
                      | Kerry's starting code
                      |
                      | >>Dim date1 As DateTime = "12/1/2005"
                      |
                      | It says by me that Option Strict Disallows direct converstion from String
                      to
                      | TimeDate.
                      |
                      | If you want can I make next time in my messages that it does not work for
                      | me, however does for you with Option Strict on in all versions.
                      |
                      | :-)
                      |
                      | Cor
                      |
                      |


                      Comment

                      • Cor Ligthert [MVP]

                        #12
                        Re: Find if date falls between 2 dates??

                        Jay,
                        [color=blue]
                        >However you are correct, his initialization code is not does not work.
                        > Dim date1 As DateTime = "12/1/2005"
                        >Which is where I would use
                        > Dim date1 As DateTime = #12/1/2005#[/color]

                        Did I give any comments on your answer?

                        Although I thought in this one that we had agreed that we would than use in
                        the newsgroup.
                        Dim date1 as DateTime = new DateTime(2005,1 2,1)

                        What is in the USA accoording to the Nebraska rules for official websites
                        what I find a good decission from that state.

                        I only told as well in my message that I would use most probably the ticks
                        in this simple question.
                        There will be other circumstances where I will use the range.

                        :-)

                        Cor


                        Comment

                        • Herfried K. Wagner [MVP]

                          #13
                          Re: Find if date falls between 2 dates??

                          "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> schrieb:

                          I assume that
                          [color=blue][color=green]
                          >> Dim date1 As DateTime = #12/1/2005#[/color][/color]

                          can be resolved at compile time, while
                          [color=blue]
                          > Dim date1 as DateTime = new DateTime(2005,1 2,1)[/color]

                          cannot.

                          --
                          M S Herfried K. Wagner
                          M V P <URL:http://dotnet.mvps.org/>
                          V B <URL:http://classicvb.org/petition/>

                          Comment

                          • Jay B. Harlow [MVP - Outlook]

                            #14
                            Re: Find if date falls between 2 dates??

                            | Did I give any comments on your answer?
                            Shakes Head, don't start Cor! Obviously you commented on my response, hence
                            I was answering your comments. You can say you didn't all you want, the
                            transcript is clearly in any number of servers..

                            I consider this the end of this discussion.


                            --
                            Hope this helps
                            Jay [MVP - Outlook]
                            ..NET Application Architect, Enthusiast, & Evangelist
                            T.S. Bradley - http://www.tsbradley.net


                            "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
                            news:uAc8F5mAGH A.436@TK2MSFTNG P10.phx.gbl...
                            | Jay,
                            |
                            | >However you are correct, his initialization code is not does not work.
                            | > Dim date1 As DateTime = "12/1/2005"
                            | >Which is where I would use
                            | > Dim date1 As DateTime = #12/1/2005#
                            |
                            | Did I give any comments on your answer?
                            |
                            | Although I thought in this one that we had agreed that we would than use
                            in
                            | the newsgroup.
                            | Dim date1 as DateTime = new DateTime(2005,1 2,1)
                            |
                            | What is in the USA accoording to the Nebraska rules for official websites
                            | what I find a good decission from that state.
                            |
                            | I only told as well in my message that I would use most probably the ticks
                            | in this simple question.
                            | There will be other circumstances where I will use the range.
                            |
                            | :-)
                            |
                            | Cor
                            |
                            |


                            Comment

                            • Cor Ligthert [MVP]

                              #15
                              Re: Find if date falls between 2 dates??

                              Jay,

                              Cor, Shakes again his head to shows again his other cheek,
                              [color=blue]
                              > Shakes Head, don't start Cor! Obviously you commented on my response,
                              > hence
                              > I was answering your comments.[/color]
                              [color=blue]
                              > I consider this the end of this discussion.
                              >[/color]
                              In almost all those discussions where you wrote this, you have started the
                              discussions as as well in this messagethread.

                              Therefore I find messages from you to let others judge so strange. You
                              started a message to me telling my answer was wrong. Than you write a
                              message where you in the middle admit that I was right, however than with a
                              part in it on which you reply forever if I had written it, in which way that
                              it shoud be better.

                              I showed you that in not any discussing way and than again this for me very
                              derogatory message.

                              Why do you think that you are the one who is without mistakes in this world.

                              It is easy to check.

                              Cor


                              Comment

                              Working...