How can I compare twice in VB.net

I want to compare twice in VB.net:

I have 1:42:21 PM and I want it to be compared with TimeOfDay in VB.net, how can I do this?

+4
source share
5 answers
New DateTime(1, 1, 1, 13, 42, 21) > TimeOfDay 

Or you can enclose the DateTime expression in # characters:

 TimeOfDay > #1:42:21 PM# 
+7
source

Show the time difference in hours, minutes and seconds

 Dim TimeEnd As DateTime = #5:00:00 PM# Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim span As System.TimeSpan = TimeEnd.TimeOfDay - DateTime.Now.TimeOfDay Label1.Text = span.Hours & "hr:" & span.Minutes & "min:" & span.Seconds & "sec" End Sub 
+6
source

You must define the input time format and then call the ToString () method on your vb.net object, entering the same format.

So, for example, if your input format is h: mm: ss tt, it seems, in your case, one of the methods:

 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 sort of comparison, you should use the DateTime.Parse () function to convert the input date to a DateTime object. Then you can simply use> 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 
+3
source
 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 this something similar to what you are looking for?

0
source

To compare the time portion of two DateTime values:

 Dim TimeStart as DateTime = #1:42:21 PM# Dim TimeEnd as DateTime = #2:00:00 PM# If TimeStart.TimeOfDay < TimeEnd.TimeOfDay Then Console.WriteLine("TimeStart is before TimeEnd") End If 
0
source

Source: https://habr.com/ru/post/1306404/


All Articles