vb.net 如何在vb.net中比较两个时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25247049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two time in vb.net
提问by user1761176
I have 1 String in VB.net
我在 VB.net 中有 1 个字符串
Dim str As String = "2014/08/15 19:45"
I have 2 time
我有2次
Dim startDate As String = "6:30"
Dim endDate As String = "22:00"
How to compare "str" with "startDate" & "endDate" ?
如何比较“str”与“startDate”和“endDate”?
回答by Tim Schmelter
First, you have strings not DateTimes or TimeSpan. But you need both if you want to compare them. So use Date.Parse, Date.ParseExact(or the ...TryParseversions to check invalid data):
首先,您的字符串不是 DateTimes 或 TimeSpan。但是如果你想比较它们,你需要两者。所以使用Date.Parse, Date.ParseExact(或...TryParse检查无效数据的版本):
Dim str As String = "2014/08/15 19:45"
Dim dt1 = Date.ParseExact(str, "yyyy/MM/dd HH:mm", CultureInfo.InvariantCulture)
Dim startDate As String = "6:30"
Dim tsStart = TimeSpan.Parse(startDate)
Dim endDate As String = "22:00"
Dim tsEnd = TimeSpan.Parse(endDate)
Now you can compare them:
现在你可以比较它们:
If dt1.TimeOfDay >= tsStart AndAlso dt1.TimeOfDay <= tsEnd Then
' Yes, 19:45 is between 6:30 and 22:00 '
End If
回答by wottle
First, you should be using dates to store the values. Then it's a simple matter of checking the time components. Note that the implementation below is inclusive (i.e. if the time is exactly the start or end time, it will print the message.
首先,您应该使用日期来存储值。然后就是检查时间分量的简单问题。请注意,下面的实现是包含性的(即如果时间正好是开始或结束时间,它将打印消息。
Dim dateToCheck As Date = Date.Parse("2014/08/15 19:45")
' The date portion doesn't matter, so I'm just using 1/1/1970.
Dim startTimeDate As Date = New Date(1970, 1, 1, 6, 30, 0)
Dim endTimeDate As Date = New Date(1970, 1, 1, 22, 0, 0)
Dim afterOrEqualToStartTime As Boolean = (dateToCheck.Hour >= startTimeDate.Hour) And (dateToCheck.Minute >= startTimeDate.Minute) And (dateToCheck.Second >= startTimeDate.Second)
Dim beforeOrEqualToEndTime As Boolean = (dateToCheck.Hour <= endTimeDate.Hour) And (dateToCheck.Minute <= endTimeDate.Minute) And (dateToCheck.Second <= endTimeDate.Second)
If (afterOrEqualToStartTime And beforeOrEqualToEndTime) Then
MsgBox("Date is between the start and end times")
End If
回答by patovega
Dim str As String = "2014/08/15 19:45"
Dim time As DateTime = DateTime.Parse(str)
'here get the hour and minutes, and now you can compare with the others
Dim tmpTime As DateTime = time.ToString("t")
回答by rheitzman
Comparing time is difficult using Date variables.
使用日期变量比较时间很困难。
Using a data difference, with a tolerance, is one way to deal with binary time.
使用具有容差的数据差异是处理二进制时间的一种方法。
I use strings:
我使用字符串:
Dim dt As Date = "2014/08/15 22:45"
Dim s As String = dt.ToString("H:MM")
If s > "06:30" And s <= "22:00" Then
MsgBox("yes")
Else
MsgBox("no")
End If
note the leading 0 in 06:30 in the sample.
请注意示例中 06:30 中的前导 0。

