简体   繁体   English

在C#中比较DateTimes会产生意外结果

[英]Comparing DateTimes in C# producing unexpected results

Im trying to compare if a runtime variable is within now and one minute from now. 我正在尝试比较运行时变量是否在现在以及从现在开始一分钟内。 Here is the code: 这是代码:

if (RunTime.ToUniversalTime() >= now.ToUniversalTime() && 
    RunTime.ToUniversalTime() < now.AddMinutes(1).ToUniversalTime())
{
    //Do Stuff 
}

However this evaluates to true a minute before I would expect. 但是,这比我预期的要早一分钟。 for instance if I have my runtime as 9:30 it evaluates to true from 9:29 to 9:30 not 9:30 - 9:31. 例如,如果我的运行时间为9:30,则它从9:29到9:30的评估结果为true,而不是9:30-9:31。 What Am I missing here? 我在这里想念什么? The way I understand the code is that it would evaluate to true when runtime is greater than or equal to now and less than now plus one minute. 我理解代码的方式是,当运行时大于或等于现在且小于现在加上一分钟时,它将评估为true。

RunTime.ToUniversalTime() >= now.ToUniversalTime() && 
RunTime.ToUniversalTime() < now.AddMinutes(1).ToUniversalTime()

Let us rewrite this condition: 让我们重写这种情况:

now.ToUniversalTime() <= RunTime.ToUniversalTime() &&
now.ToUniversalTime() > RunTime.AddMinutes(-1).ToUniversalTime()

So when RunTime is 9:30, then condition should be true from RunTime.AddMinutes(-1) (9:29) exclusive to RunTime (9:30) inclusive. 因此,当RunTime为9:30时,条件应为RunTime.AddMinutes(-1) (9:29)(不包括RunTime (9:30))为真。 Just like you say it is in your post. 就像您说的那样。

If you what that condition be true from 9:30 to 9:31, than you should use different condition: 如果您从9:30到9:31满足该条件,则应使用其他条件:

now.ToUniversalTime() >= RunTime.ToUniversalTime() &&
now.ToUniversalTime() < RunTime.AddMinutes(1).ToUniversalTime()

Since you're using DateTime.Now you're never going to land precisely on the exact same time. 由于您使用的是DateTime.Now您永远不会精确地在同一时间着陆。 Consider these two cases: 考虑以下两种情况:

Case 1 (what you're experiencing)
now:     9:29:01
runtime: 9:30:00
now+1:   9:30:01

Case 2 (what you want but doesn't work)
now:     9:30:01
runtime: 9:30:00
now+1:   9:31:01

Try setting your now variable explicitly and make some logical tables of what should happen. 尝试显式设置now变量,并制作一些应该发生的逻辑表。 You will get better insight to the rule you want to create. 您将对要创建的规则有更好的了解。

The following code works as expected (ie it prints "Second true" and not "First true"): 下面的代码按预期工作(即,打印“第二个真实”而不是“第一个真实”):

DateTime nine29  = new DateTime(2015, 01, 01, 09, 29, 00);
DateTime nine30  = new DateTime(2015, 01, 01, 09, 30, 00);
DateTime runtime = nine30;

if (runtime.ToUniversalTime() >= nine29.ToUniversalTime() && runtime.ToUniversalTime() < nine29.AddMinutes(1).ToUniversalTime())
    Console.WriteLine("First true");

if (runtime.ToUniversalTime() >= nine30.ToUniversalTime() && runtime.ToUniversalTime() < nine30.AddMinutes(1).ToUniversalTime())
    Console.WriteLine("Second true");

Therefore we must assume that you are mistaken when you say "if I have my runtime as 9:30 it evaluates to true from 9:29 to 9:30 not 9:30 - 9:31." 因此,当您说“如果我的运行时间为9:30时,它的计算结果是从9:29到9:30而不是9:30-9:31,则为真” ,我们必须假定您是错误的

Put all your times into variables and print them out to the debug window or the console, or run it under the debugger and inspect them. 将所有时间都放入变量中,然后将其打印到调试窗口或控制台中,或者在调试器下运行它并进行检查。 And pay attention to the seconds! 并注意秒!

I like to use type TimeSpan when doing date-time comparisons. 我喜欢在进行日期时间比较时使用TimeSpan类型。

TimeSpan timeSpan = Runtime.ToUniversalTime().Subtract(now.ToUniversalTime());

if (timeSpan > 0 && timeSpan <= 1)
{
   //Code here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM