2010-04-08 307 views
4

比較兩個時間我想在VB.net比較兩個時期:我怎樣才能在VB.net

我下午1點42分21秒,我想它在VB.net的TimeOfDay與比較怎麼能我這樣做?

回答

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

或者你可以在#跡象包圍DateTime表達:

TimeOfDay > #1:42:21 PM# 
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 

是沿着這是你正在尋找的東西線?

3

你會找出你的輸入時間的格式,然後調用ToString()方法的vb.net對象上,把相同的格式英寸

因此,舉例來說,如果你的輸入格式爲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 
0

要比較兩個日期時間值的時間部分:

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 
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 
+1

一些解釋會很好。 – pedrofurla 2012-10-15 06:18:00