:
: If I want to check to see if it's after "11:36 pm" what would I write?
:
: I'm sure it's easy but I'm getting tired of having to work with
: dates and times. Sometimes I just want time or date. And to be able
: to do comparisons on them.
I found working with the DateTime structure a bit annoying at first, but I
eventually became comfortable with it. You may also want to look at the
Timespan structure.
Still, if all you really want is do is to work with times irrespective of
any given date (e.g., doing a compare to see if one event took place later
in the day than a second event that occurred on another date), you might try
rolling your own Time class. Here is a quick starter Class you can play
with. Basically, it just wraps a DateTime object and always sets the date
portion to the same value (January 1, 2000). Any comparisons between two
Time objects then are only looking at the time portion of those objects.
The class is immutable: once you instantiate an instance, you cannot change
it - you can extract the hours, minutes, seconds or milliseconds, but you
cannot modify those values. Also, you can compare (>, <, >=, <=, = or <>)
two Time objects or a Time object and a time string. And finally, the
ToString, Equals and GetHashCode functions have been overriden from the base
Object implementation.
Finally, while I did do some testing of this code (see the Module at the
bottom for some test cases), don't assume this is error free (for example, I
made no provision for locality when using string values - that may or may
not be an issue for you). Test this for your own purposes before using it.
(Oh, the line continuation below was so this fit in this posting without
wrapping)
================================================================
Option Strict
Imports Microsoft.VisualBasic
Imports System
Public Class Time
Private dt As DateTime
Public Sub New
Me.New(Now)
End Sub
Public Sub New(d As DateTime)
dt = New DateTime(2000, 1, 1, d.hour, d.minute, d.second, d.millisecond)
End Sub
Public Sub New (hour As Integer, minute As Integer, second As Integer)
dt = New DateTime(2000, 1, 1, hour, minute, second)
End Sub
Public Sub New (hour As Integer, minute As Integer, second As Integer, _
millisecond As Integer)
dt = New DateTime(2000, 1, 1, hour, minute, second, millisecond)
End Sub
Public Sub New (t As String)
dt = DateTime.Parse("1/1/2000 " & t)
End Sub
Public Readonly Property Hour As Integer
Get
Return dt.Hour
End Get
End Property
Public Readonly Property Minute As Integer
Get
Return dt.Minute
End Get
End Property
Public Readonly Property Second As Integer
Get
Return dt.Second
End Get
End Property
Public Readonly Property Millisecond As Integer
Get
Return dt.Millisecond
End Get
End Property
Public Shared Operator > (t1 As Time, t2 As Time) As Boolean
Return t1.dt > t2.dt
End Operator
Public Shared Operator >= (t1 As Time, t2 As Time) As Boolean
Return t1.dt >= t2.dt
End Operator
Public Shared Operator < (t1 As Time, t2 As Time) As Boolean
Return t1.dt < t2.dt
End Operator
Public Shared Operator <= (t1 As Time, t2 As Time) As Boolean
Return t1.dt <= t2.dt
End Operator
Public Shared Operator = (t1 As Time, t2 As Time) As Boolean
Return t1.dt = t2.dt
End Operator
Public Shared Operator <> (t1 As Time, t2 As Time) As Boolean
Return t1.dt <> t2.dt
End Operator
Public Shared Operator > (t1 As Time, t2 As String) As Boolean
Return t1.dt > New Time(t2).dt
End Operator
Public Shared Operator >= (t1 As Time, t2 As String) As Boolean
Return t1.dt >= New Time(t2).dt
End Operator
Public Shared Operator < (t1 As Time, t2 As String) As Boolean
Return t1.dt < New Time(t2).dt
End Operator
Public Shared Operator <= (t1 As Time, t2 As String) As Boolean
Return t1.dt <= New Time(t2).dt
End Operator
Public Shared Operator = (t1 As Time, t2 As String) As Boolean
Return t1.dt = New Time(t2).dt
End Operator
Public Shared Operator <> (t1 As Time, t2 As String) As Boolean
Return t1.dt <> New Time(t2).dt
End Operator
Public Overrides Function ToString() As String
Return dt.ToString("T")
End Function
Public Overrides Function GetHashCode() As Integer
Return dt.GetHashCode
End Function
Public Overloads Function Equals(T As Time) As Boolean
If T Is Nothing Then
Return False
End If
Return dt.equals(T.dt)
End Function
Public Overrides Function Equals(obj As Object) As Boolean
If obj Is Nothing Then
Return False
End If
If Not Me.GetType.Equals(obj.GetType) Then
Return False
End If
Dim obj2 As Time = DirectCast(obj, Time)
If Not Object.equals(obj, obj2) Then
Return False
End If
Return dt= obj2.dt
End Function
Public Shared Overloads Function Equals(T1 As Time, T2 As Time) As Boolean
If T1 Is Nothing OrElse T2 Is Nothing Then
Return False
End If
Return T1 = T2
End Function
End Class
Public Module [module]
Public Sub Main
Dim T1 As Time
Dim T2 As Time
Dim T3 As Time
Dim T4 As Time
T1 = New Time(1, 2, 3)
T2 = New Time(2, 3, 4)
T3 = New Time(1, 2, 3)
T4 = New Time(1, 2, 3, 4)
Show(New Time)
Show(New Time(New DateTime(2001, 1, 1)))
Show(T1)
Show(New Time(1, 2, 3))
Show(New Time(1, 2, 3, 4))
Show(New Time("1:2:3.4 AM"))
Compare(T1, T2)
Compare(T2, T1)
Compare(T1, T3)
Compare(T4, "1:2:3 AM")
Compare(T4, "1:2:3.004 AM")
Compare(T4, "1:2:3.4 AM")
Console.WriteLine
Console.WriteLine(T1.Equals(Nothing))
Console.WriteLine(T1.Equals(""))
Console.WriteLine(T1.Equals(CType(T1, Object)))
Console.WriteLine(T1.Equals(CType(T2, Object)))
Console.WriteLine(T1.Equals(CType(T3, Object)))
Console.WriteLine(Time.Equals(T1, T1))
Console.WriteLine(Time.Equals(T1, T2))
Console.WriteLine(Time.Equals(T1, T3))
Console.WriteLine(Time.Equals(T1, Nothing))
Console.WriteLine(Time.Equals(Nothing, T2))
End Sub
Private Sub Show(t As Time)
Console.WriteLine
Console.WriteLine(T)
Console.WriteLine("Hour: " & T.Hour)
Console.WriteLine("Minute: " & T.Minute)
Console.WriteLine("Second: " & T.Second)
Console.WriteLine("Milliseconds: " & T.Millisecond)
Console.WriteLine(T.GetHashCode)
End Sub
Private Sub Compare(T1 As Time, T2 As Time)
Console.WriteLine
Console.WriteLine(T1 > T2)
Console.WriteLine(T1 < T2)
Console.WriteLine(T1 >= T2)
Console.WriteLine(T1 <= T2)
Console.WriteLine(T1 = T2)
Console.WriteLine(T1 <> T2)
End Sub
Private Sub Compare(T1 As Time, T2 As String)
Console.WriteLine
Console.WriteLine(T1 > T2)
Console.WriteLine(T1 < T2)
Console.WriteLine(T1 >= T2)
Console.WriteLine(T1 <= T2)
Console.WriteLine(T1 = T2)
Console.WriteLine(T1 <> T2)
End Sub
End Module
================================================================
Ralf