vb.net 检查日期是否介于或等于其他两个日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24072072/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:40:08  来源:igfitidea点击:

Check if date is between or equal to two other dates

vb.net

提问by alwaysVBNET

I'm trying to check if a date is between two other dates. The two other dates are coming from two DatePickers formatted as dd/mm/yyyy. My code below works but if my searching date is i.e. equal to the from date I get a "Not between" message. If I search for date 17/05/2013 and I set the ranges to bet from: 17/05/2013 and to: 17/05/2013 I want to get a "Between" message. Any ideas?

我试图检查一个日期是否在其他两个日期之间。另外两个日期来自两个格式为 dd/mm/yyyy 的 DatePicker。我下面的代码有效,但如果我的搜索日期等于起始日期,我会收到“不在之间”消息。如果我搜索日期 17/05/2013 并将投注范围设置为:17/05/2013 至:17/05/2013,我想收到“之间”消息。有任何想法吗?

Dim str As String = "srt_inlbp_20130517"
Dim sString As String

sString = str.Substring(str.Length - 8)
Dim dTableDate As Date = Date.ParseExact(str.Substring(str.Length - 8), "yyyyMMdd", Nothing)

Label9.Text = dTableDate

If ((dTableDate >= dFromDate.Value) And (dTableDate <= dToDate.Value)) Then
        MsgBox("between")
Else
        MsgBox("not between")
End If

回答by Enigmativity

I have rewritten your code in this testable format:

我以这种可测试的格式重写了您的代码:

Dim str As String = "srt_inlbp_20130517"

Dim dTableDate As Date = _
    Date.ParseExact(str.Substring(str.Length - 8), "yyyyMMdd", Nothing)

Dim dFromDate As New DateTime(2013, 5, 17)
Dim dToDate As New DateTime(2013, 6, 17)

If ((dTableDate >= dFromDate) And (dTableDate <= dToDate)) Then
    Console.WriteLine("between")
Else
    Console.WriteLine("not between")
End If

I get the result "between" so I can only suspect that your dFromDate.Valueand/or dToDate.Valueare not as you expect them to be. Can you check this please?

我得到了“介于”之间的结果,所以我只能怀疑你的dFromDate.Value和/或dToDate.Value不是你期望的那样。你能检查一下吗?

回答by Sam Axe

Try to write your comparators like you are reading a number line (e.g. always use a less-than sign).

尝试像阅读数轴一样编写比较器(例如,始终使用小于号)。

Dim questionableDate As Date = New Date(2013, 05, 17)
Dim fromDate As Date = New Date(2013, 05, 17)
Dim toDate As Date = New Date(2013, 05, 17)

If (fromDate <= questionableDate) AndAlso (questionableDate <= toDate) Then
    MsgBox("between")
Else
    MsgBox("not between")
End If

You'll see that by using the number line approach you can easily determine what the code is doing. The from date and to date are at the ends with the questionable date sandwiched in the middle.

您将看到,通过使用数轴方法,您可以轻松确定代码在做什么。起始日期和截止日期在末尾,可疑日期夹在中间。

Note

笔记

If the code above does not wield the results you expect once you integrate it, then you will need to set some breakpoints and step through your code to see exactly what values your variables have.

如果上面的代码在集成后没有得到您期望的结果,那么您将需要设置一些断点并逐步执行您的代码以准确查看您的变量具有哪些值。