473,748 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find if date falls between 2 dates??

Anyone have some code that will do this?

Dave

Dec 13 '05 #1
18 38244
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
"df********@hot mail.com" wrote:
Anyone have some code that will do this?

Dave

Dec 13 '05 #2
Dave,

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

http://msdn.microsoft.com/library/de...tickstopic.asp

I hope this helps,

Cor
Dec 13 '05 #3
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
<df********@hot mail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
| Anyone have some code that will do this?
|
| Dave
|
Dec 13 '05 #4
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
<df********@hot mail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
| Anyone have some code that will do this?
|
| Dave
|

Dec 14 '05 #5
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" <De****@discuss ions.microsoft. com> wrote in message
news:C8******** *************** ***********@mic rosoft.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
| >
| >
| > <df********@hot mail.com> wrote in message
| > news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
| > | Anyone have some code that will do this?
| > |
| > | Dave
| > |
| >
| >
| >
Dec 14 '05 #6
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" <De****@discuss ions.microsoft. com> schreef in bericht
news:C8******** *************** ***********@mic rosoft.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
<df********@hot mail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
| Anyone have some code that will do this?
|
| Dave
|

Dec 14 '05 #7
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:
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" <De****@discuss ions.microsoft. com> schreef in bericht
news:C8******** *************** ***********@mic rosoft.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
<df********@hot mail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
| Anyone have some code that will do this?
|
| Dave
|


Dec 15 '05 #8
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]" <no************ @planet.nl> wrote in message
news:OI******** *****@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" <De****@discuss ions.microsoft. com> schreef in bericht
| news:C8******** *************** ***********@mic rosoft.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
| >>
| >>
| >> <df********@hot mail.com> wrote in message
| >> news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
| >> | Anyone have some code that will do this?
| >> |
| >> | Dave
| >> |
| >>
| >>
| >>
|
|
Dec 15 '05 #9
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
Dec 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
21368
by: bradleyc | last post by:
How would you calculate the difference between two dates?
7
14932
by: Hieronimus | last post by:
Hi all I inherited a server running MySQL 4.* from a previous developer. Now, the records on this database might be what I want, or they might just be test data. I think my only chance of finding out if this is the real thing is to find the date and time of the last insert into the database. How can I do that?
1
2262
by: DeanMiller | last post by:
I was wondering if there is an easy way to check if a date falls in a given selection range. I have the selection range already made and I want to be able to take a given date and see if it falls within that range. Thank you, -Dean Miller
3
1277
by: savakis | last post by:
hello every one. I can not get this to find the same date on a spread sheet? what is wrong with this. I put 2 dates in the same. it can find any text though. Dim Var1 As Range Set Var1 = Workbooks("Visits Numbers.xls").Worksheets("Visit Nos").Range("AB209") Set foundcell1 = ThisWorkbook.Worksheets("Visit Nos").Range("AB210:AB65536").find(Var1)
0
1037
by: ncrawford | last post by:
Hello All This is my first posting....and thanks in anticipation of help. Using MS Access 2000 I am recording sick days absence for individual employees. A StartDate and EndDate are entered and the system calculates how many days leave taken for this period. In addition the system looks back 12 months from the and calculates the sick days taken to date. e.g. StartDate is 16/07/07 - system looks back to 17/07/06 and calculates how...
13
4490
by: citizenprice | last post by:
I have three fields in a Union Access Query (ID, Begin_Date, End_Date). I want to modify the query to include a parameter date to be entered by the user and produce a result if the parameter date falls within the range of Begin_Date to End_Date. Any ideas?
1
4818
by: karimufeed | last post by:
I am working on an access project for pension calculation. I want to generate the retirement date automatically at the age of 60 years while filling the date of Birth. i.e. if the Date of birth is 15/1/1950 then the date of retirement will be 31/1/2010 and if the date of birth is 01/1/1950 then the date of retirement will be 31/12/2009 one day earlier. Kindly help me how to generate the date of retirement by using VB code in an access project.
4
3589
by: Phil Davis | last post by:
Hi I'm struggling to find an elegant way of determining whether the date from a record falls within a number of date boundaries ranges. For example in a separate (date boundaries) table I code the "Winter Season" running from 1/11 to 28/2 and the "Summer Season" running from 1/4 to 31/7. I have day1, day2... and month1, month2... fields for each season I'm try to build something like Select * from tab1 where date between 1/11/year(date)...
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.