D
dfetrow410
Anyone have some code that will do this?
Dave
Dave
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Jay B. Harlow said: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/Generics/genericRange.aspx
I have a TimeRange defined at (usable in VS 2002, 2003 & 2005):
http://www.tsbradley.net/Cookbook/Patterns/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
| Anyone have some code that will do this?
|
| Dave
|
Dennis said: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 said: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/Generics/genericRange.aspx
I have a TimeRange defined at (usable in VS 2002, 2003 & 2005):
http://www.tsbradley.net/Cookbook/Patterns/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
| Anyone have some code that will do this?
|
| Dave
|
Cor Ligthert said: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 said: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 said: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/Generics/genericRange.aspx
I have a TimeRange defined at (usable in VS 2002, 2003 & 2005):
http://www.tsbradley.net/Cookbook/Patterns/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
| Anyone have some code that will do this?
|
| Dave
|
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.


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#

Dim date1 as DateTime = new DateTime(2005,12,1)

Shakes Head, don't start Cor! Obviously you commented on my response,
hence
I was answering your comments.
In almost all those discussions where you wrote this, you have started theI consider this the end of this discussion.
And?I assume that
can be resolved at compile time, while
cannot.
--
I assume that
can be resolved at compile time, while
cannot.
| I showed you that in not any discussing way and than again this for me
very
| derogatory message.
Derogatory how?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.