tags:

views:

73

answers:

3

i want to compare two times in vb.net i.e

i have 1:42:21 PM and i want it to compare with TimeOfDay in vb.net how can i do that....

+4  A: 
New DateTime(1, 1, 1, 13, 42, 21) > TimeOfDay

Or you can enclose a DateTime expression in # signs:

TimeOfDay > #1:42:21 PM#
Mehrdad Afshari
A: 
The following sample function can be used to compare time
Function comTime()
Dim t1 As Integer = DateTime.Now.TimeOfDay.Milliseconds
Dim t2 As Integer = DateTime.Now.AddHours(1).Millisecond
If (t1 > t2) Then
MessageBox.Show("t1>t2")
ElseIf (t1 = t2) Then
MessageBox.Show("t1=t2")
Else
MessageBox.Show("t2>t1")
End If
End Function

is it something along the lines of this that you are looking for?

Justin Gregoire
A: 

You'd work out the format of your input time, and then call the ToString() method on your vb.net object, putting the same format in.

So for example, if your input format is h:mm:ss tt as it appears to be in your case, one method would be to do:

Dim compareTime As String = "1:42:21 PM"

If compareTime = DateTime.Now.ToString("h:mm:ss tt") Then

   ' The times match

End If

If you want to do some kind of comparison, you should use the DateTime.Parse() function to convert your input date into a DateTime object. Then you can simply use the > or < signs:

Dim myCompareTime As DateTime = DateTime.Parse("1:42:21 PM")

If myCompareTime.TimeOfDay > DateTime.Now.TimeOfDay Then

    ' Compare date is in the future!

End If
SLC