By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,011 Members | 2,967 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,011 IT Pros & Developers. It's quick & easy.

Find if date falls between 2 dates??

P: n/a
Anyone have some code that will do this?

Dave

Dec 13 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
| 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

P: n/a
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

P: n/a
>
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.