如何在VB.net中进行两次比较

时间:2010-04-08 20:23:31

标签: vb.net timeofday

我想在VB.net中进行两次比较:

我有1:42:21 PM我希望它与VB.net中的TimeOfDay进行比较我该怎么做?

5 个答案:

答案 0 :(得分:8)

New DateTime(1, 1, 1, 13, 42, 21) > TimeOfDay

或者您可以在DateTime符号中附上#表达式:

TimeOfDay > #1:42:21 PM#

答案 1 :(得分:6)

以小时,分钟和秒显示时差

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

答案 2 :(得分:3)

您可以计算出输入时间的格式,然后在vb.net对象上调用ToString()方法,并使用相同的格式。

例如,如果您的输入格式为h:mm:ss tt,就像您的情况一样,可以采用以下方法:

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

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

   ' The times match

End If

如果要进行某种比较,则应使用DateTime.Parse()函数将输入日期转换为DateTime对象。然后你可以简单地使用>或者<体征:

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 :(得分:0)

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

这是你要找的东西吗?

答案 4 :(得分:0)

比较两个DateTime值的时间部分:

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