473,396 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Find if date falls between 2 dates??

Anyone have some code that will do this?

Dave

Dec 13 '05 #1
18 38190
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********@hotmail.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.Contains(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.CompareTo(Nothing) = 0 AndAlso
m_end.CompareTo(Nothing) = 0
Return m_start.Equals(Nothing) AndAlso m_end.Equals(Nothing)
End Get
End Property

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

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

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

Public Overrides Function GetHashCode() As Integer
Return m_start.GetHashCode() Xor m_end.GetHashCode()
End Function

Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean
If TypeOf obj Is DateRange Then
Return Equals(DirectCast(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(other.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********@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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.Contains(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.CompareTo(Nothing) = 0 AndAlso
m_end.CompareTo(Nothing) = 0
Return m_start.Equals(Nothing) AndAlso m_end.Equals(Nothing)
End Get
End Property

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

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

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

Public Overrides Function GetHashCode() As Integer
Return m_start.GetHashCode() Xor m_end.GetHashCode()
End Function

Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean
If TypeOf obj Is DateRange Then
Return Equals(DirectCast(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(other.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********@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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.Contains(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.Contains(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.CompareTo(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.CompareTo 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****@discussions.microsoft.com> wrote in message
news:C8**********************************@microsof t.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.Contains(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.CompareTo(Nothing) = 0 AndAlso
| > m_end.CompareTo(Nothing) = 0
| > Return m_start.Equals(Nothing) AndAlso m_end.Equals(Nothing)
| > End Get
| > End Property
| >
| > Public Function Contains(ByVal value As DateTime) As Boolean
| > value = value.Date
| > Return m_start.CompareTo(value) <= 0 AndAlso
value.CompareTo(m_end)
| > <= 0
| > End Function
| >
| > Public Function Contains(ByVal value As DateRange) As Boolean
| > Return Me.Contains(value.m_start) AndAlso
Me.Contains(value.m_end)
| > End Function
| >
| > Public Function Overlaps(ByVal value As DateRange) As Boolean
| > Return Me.Contains(value) OrElse value.Contains(m_start) OrElse
| > value.Contains(m_end)
| > End Function
| >
| > Public Overrides Function GetHashCode() As Integer
| > Return m_start.GetHashCode() Xor m_end.GetHashCode()
| > End Function
| >
| > Public Overloads Overrides Function Equals(ByVal obj As Object) As
| > Boolean
| > If TypeOf obj Is DateRange Then
| > Return Equals(DirectCast(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(other.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********@hotmail.com> wrote in message
| > news:11**********************@o13g2000cwo.googlegr oups.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****@discussions.microsoft.com> schreef in bericht
news:C8**********************************@microsof t.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.Contains(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.CompareTo(Nothing) = 0 AndAlso
m_end.CompareTo(Nothing) = 0
Return m_start.Equals(Nothing) AndAlso m_end.Equals(Nothing)
End Get
End Property

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

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

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

Public Overrides Function GetHashCode() As Integer
Return m_start.GetHashCode() Xor m_end.GetHashCode()
End Function

Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean
If TypeOf obj Is DateRange Then
Return Equals(DirectCast(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(other.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********@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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.CompareTo(TestDate) <= 0 AndAlso TestDate.CompareTo(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****@discussions.microsoft.com> schreef in bericht
news:C8**********************************@microsof t.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.Contains(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.CompareTo(Nothing) = 0 AndAlso
m_end.CompareTo(Nothing) = 0
Return m_start.Equals(Nothing) AndAlso m_end.Equals(Nothing)
End Get
End Property

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

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

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

Public Overrides Function GetHashCode() As Integer
Return m_start.GetHashCode() Xor m_end.GetHashCode()
End Function

Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean
If TypeOf obj Is DateRange Then
Return Equals(DirectCast(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(other.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********@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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*************@tk2msftngp13.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****@discussions.microsoft.com> schreef in bericht
| news:C8**********************************@microsof t.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.Contains(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.CompareTo(Nothing) = 0 AndAlso
| >> m_end.CompareTo(Nothing) = 0
| >> Return m_start.Equals(Nothing) AndAlso
m_end.Equals(Nothing)
| >> End Get
| >> End Property
| >>
| >> Public Function Contains(ByVal value As DateTime) As Boolean
| >> value = value.Date
| >> Return m_start.CompareTo(value) <= 0 AndAlso
| >> value.CompareTo(m_end)
| >> <= 0
| >> End Function
| >>
| >> Public Function Contains(ByVal value As DateRange) As Boolean
| >> Return Me.Contains(value.m_start) AndAlso
| >> Me.Contains(value.m_end)
| >> End Function
| >>
| >> Public Function Overlaps(ByVal value As DateRange) As Boolean
| >> Return Me.Contains(value) OrElse value.Contains(m_start) OrElse
| >> value.Contains(m_end)
| >> End Function
| >>
| >> Public Overrides Function GetHashCode() As Integer
| >> Return m_start.GetHashCode() Xor m_end.GetHashCode()
| >> End Function
| >>
| >> Public Overloads Overrides Function Equals(ByVal obj As Object) As
| >> Boolean
| >> If TypeOf obj Is DateRange Then
| >> Return Equals(DirectCast(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(other.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********@hotmail.com> wrote in message
| >> news:11**********************@o13g2000cwo.googlegr oups.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
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.CompareTo is not required as
DateTime <= works. TimeSpan.CompareTo 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]" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP10.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
|
|
Dec 16 '05 #11
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,12,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
Dec 16 '05 #12
"Cor Ligthert [MVP]" <no************@planet.nl> schrieb:

I assume that
Dim date1 As DateTime = #12/1/2005#

can be resolved at compile time, while
Dim date1 as DateTime = new DateTime(2005,12,1)


cannot.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Dec 16 '05 #13
| 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]" <no************@planet.nl> wrote in message
news:uA*************@TK2MSFTNGP10.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,12,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
|
|
Dec 16 '05 #14
Jay,

Cor, Shakes again his head to shows again his other cheek,
Shakes Head, don't start Cor! Obviously you commented on my response,
hence
I was answering your comments. I consider this the end of this discussion.

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
Dec 17 '05 #15
>
I assume that
Dim date1 As DateTime = #12/1/2005#


can be resolved at compile time, while
Dim date1 as DateTime = new DateTime(2005,12,1)


cannot.

--

And?

I find maintenance more important than a bit that is gained by this. You
should know how easy dates as #12/1/2005# can be read wrong outside the USA.

However maybe do you have a very special reason?

Cor
Dec 17 '05 #16
On 2005-12-16, Herfried K. Wagner [MVP] <hi***************@gmx.at> wrote:
"Cor Ligthert [MVP]" <no************@planet.nl> schrieb:

I assume that
Dim date1 As DateTime = #12/1/2005#


can be resolved at compile time, while
Dim date1 as DateTime = new DateTime(2005,12,1)


cannot.


Depends what you mean by "resolved". The DateTime ctor still needs to
be called at runtime in both cases. The only difference is that the
first will run the DateTime.New(ticks) constructor, which is a bit
faster than the ctor that takes year,month,day params.

Dec 17 '05 #17
Cor,
| 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.
So what's your problem here?

It seems to me you misunderstood something I stated, I further explained
myself, which I get the impression you now understand. However you seem to
have a problem with me correcting you in the first place, and then
attempting to clarify for you what I meant.

I really don't understand what your problem here is!
| I showed you that in not any discussing way and than again this for me
very
| derogatory message.
Derogatory how?
| Why do you think that you are the one who is without mistakes in this
world.
Funny, I admit right away when I make a mistake, especially in the
newsgroups! How many times have I posted "Doh!" in the newsgroups in the
last 6 months? Here's a hint on how often:

http://groups.google.com/groups?as_q...=2005&safe=off
--
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:eW**************@TK2MSFTNGP10.phx.gbl...
| Jay,
|
| Cor, Shakes again his head to shows again his other cheek,
|
| > Shakes Head, don't start Cor! Obviously you commented on my response,
| > hence
| > I was answering your comments.
|
| > I consider this the end of this discussion.
| >
| 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
|
|
Dec 18 '05 #18
Jay,
| I showed you that in not any discussing way and than again this for me
very
| derogatory message.
Derogatory how?


Cor shakes his head, he stops the discussion, he can use his time better.

(As is the standard end from Jay to Cor when it seems that Jay cannot answer
anymore with facts).

Cor

Dec 18 '05 #19

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

Similar topics

1
by: bradleyc | last post by:
How would you calculate the difference between two dates?
7
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...
1
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...
3
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 =...
0
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...
13
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...
1
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...
4
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.