在 asp.net 中比较 StartDate 和 EndDate 时的混乱

发布于 2024-12-07 11:13:16 字数 271 浏览 0 评论 0原文

在我的 asp.net 应用程序中比较 StartDate 和 EndDate 时,我有点困惑。

EndDate:    10/1/2011
StartDate:  9/30/2011

下面的 if 语句根据上面的日期值返回 true。

如果 strEndDate < strStartDate 那么

我认为 If 语句应该返回 false。这个概念应该是,如果 EndDate 早于 StartDate,则显示错误消息。

I am a bit confused when comparing StartDate and EndDate in my asp.net app.

EndDate:    10/1/2011
StartDate:  9/30/2011

The if statement below returns true, based on the date values above.

If strEndDate < strStartDate Then

I would think the If statement shoudl return false. The concept is supposed to be that if EndDate is earlier than StartDate, display an error message.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清风疏影 2024-12-14 11:13:16

我假设由于您的变量名为 strEndDatestrStartDate,因此它们是字符串,而不是 DateTime。因为它们是字符串,并且因为 '1' < '9'(字符串中的第一个字符),strEndDate 确实“小于”strStartDate

在比较之前将值转换为 DateTime

If DateTime.Parse(strEndDate) < DateTime.Parse(strStartDate) Then
    '...
End If

I'm assuming that since your variables are called strEndDate and strStartDate that they're strings, not DateTimes. Since they're strings, and since '1' < '9' (the first characters in the strings), strEndDate is indeed "less than" strStartDate.

Convert the values to DateTime before comparing:

If DateTime.Parse(strEndDate) < DateTime.Parse(strStartDate) Then
    '...
End If
星光不落少年眉 2024-12-14 11:13:16

如果这些是 DateTime 变量,则该代码的计算结果应为 true。如果不是,您应该将它们转换为 DateTime。

DateTime start = DateTime.Parse(strStartDate);
DateTime close = DateTime.Parse(strEndDate);

if (close > start)
    //do something

您还可以像这样比较它们:

if ((close - start).TotalDays > 0)
    //do something

正如 Rick Schott 指出的,您还可以使用 DateTime.Compare

If those are DateTime variables, that code should evaluate to true. If not, you should convert them to DateTime.

DateTime start = DateTime.Parse(strStartDate);
DateTime close = DateTime.Parse(strEndDate);

if (close > start)
    //do something

You can also compare them like this:

if ((close - start).TotalDays > 0)
    //do something

As Rick Schott pointed out, you can also use DateTime.Compare

极度宠爱 2024-12-14 11:13:16

您应该使用 DateTime.Compare

Dim date1 As Date = DateTime.Parse(strStartDate)
Dim date2 As Date = DateTime.Parse(strEndDate)
Dim result As Integer = DateTime.Compare(date1, date2)
Dim relationship As String

If result < 0 Then
   relationship = "is earlier than"
ElseIf result = 0 Then
   relationship = "is the same time as"         
Else
   relationship = "is later than"
End If

Console.WriteLine("{0} {1} {2}", date1, relationship, date2)
' The example displays the following output:
'    8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM

You should be using DateTime.Compare.

Dim date1 As Date = DateTime.Parse(strStartDate)
Dim date2 As Date = DateTime.Parse(strEndDate)
Dim result As Integer = DateTime.Compare(date1, date2)
Dim relationship As String

If result < 0 Then
   relationship = "is earlier than"
ElseIf result = 0 Then
   relationship = "is the same time as"         
Else
   relationship = "is later than"
End If

Console.WriteLine("{0} {1} {2}", date1, relationship, date2)
' The example displays the following output:
'    8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM
梦在深巷 2024-12-14 11:13:16

不要将它们作为字符串进行比较,而将它们作为日期进行比较。

Don't compare them as strings, compare them as dates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文