1

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. 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.

KDecker
  • 6,928
  • 8
  • 40
  • 81
user3839756
  • 793
  • 1
  • 9
  • 22
  • I assume `now` equals `DateTime.Now` just before the code posted? – KDecker Apr 21 '16 at 16:57
  • Yes, I just have now set as a variable because this is in a foreach loop and I want now to be the same for each item that is iterated. – user3839756 Apr 21 '16 at 16:59
  • What time zones are you working with (are they both the same)? Does the `ToUniversalTime()` calls give you the values you are expecting? – krillgar Apr 21 '16 at 16:59
  • You are using two different now times. They may be close but not exactly the same. Always set now to a variable so both now are the same. – jdweng Apr 21 '16 at 17:00
  • 1
    @jdweng `now` is a variable. – KDecker Apr 21 '16 at 17:01
  • @krill I only need to work with PT, although I'm covering to Universal just to be safe. Universal is 7 hours before pt but this should not make a difference because all values are being converted. – user3839756 Apr 21 '16 at 17:04
  • If you put a breakpoint in there, what are each of the values showing you? If this is on a server, and the `RunTime` is coming from the client, there could be some conversion happening. – krillgar Apr 21 '16 at 17:06
  • Just ran the code with a break point. now.ToUniversalTime() evaluates to: {4/21/2016 5:09:00 PM} and Runtime.ToUniversalTime() evaluates to: {4/21/2016 5:10:00 PM} – user3839756 Apr 21 '16 at 17:09

4 Answers4

3
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. 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:

now.ToUniversalTime() >= RunTime.ToUniversalTime() &&
now.ToUniversalTime() < RunTime.AddMinutes(1).ToUniversalTime()
user4003407
  • 21,204
  • 4
  • 50
  • 60
2

Since you're using DateTime.Now you're never going to land precisely on the exact same time. 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. You will get better insight to the rule you want to create.

Steve Wakeford
  • 331
  • 1
  • 8
1

The following code works as expected (i.e 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."

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!

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Try with that value: `DateTime nine2901 = new DateTime(2015, 01, 01, 09, 29, 01);` – user4003407 Apr 21 '16 at 17:23
  • Here I use a breakpoint when my if evaluates to true: ?RunTime.ToUniversalTime() {4/21/2016 5:33:00 PM} Date: {4/21/2016 12:00:00 AM} dateData: 5247654586227387904 Day: 21 DayOfWeek: Thursday DayOfYear: 112 Hour: 17 InternalKind: 4611686018427387904 InternalTicks: 635968567800000000 Kind: Utc Millisecond: 0 Minute: 33 Month: 4 Second: 0 Ticks: 635968567800000000 TimeOfDay: {17:33:00} Year: 2016 – user3839756 Apr 21 '16 at 17:33
  • ?now.ToUniversalTime() {4/21/2016 5:32:03 PM} Date: {4/21/2016 12:00:00 AM} dateData: 5247654585659202448 Day: 21 DayOfWeek: Thursday DayOfYear: 112 Hour: 17 InternalKind: 4611686018427387904 InternalTicks: 635968567231814544 Kind: Utc Millisecond: 181 Minute: 32 Month: 4 Second: 3 Ticks: 635968567231814544 TimeOfDay: {17:32:03.1814544} Year: 2016 – user3839756 Apr 21 '16 at 17:34
  • So yes It really is evaluating to true when now is < Runtime – user3839756 Apr 21 '16 at 17:34
  • @user3839756 *So yes It really is evaluating to true when now is < Runtime* You ask it yourself: `RunTime.ToUniversalTime() >= now.ToUniversalTime()`. – user4003407 Apr 21 '16 at 17:36
  • Excuse me I meant to say when Runtime < now. – user3839756 Apr 21 '16 at 17:38
  • @user3839756 Runtime (4/21/2016 5:33:00 PM) < now (4/21/2016 5:32:03 PM)? – user4003407 Apr 21 '16 at 17:40
  • 1
    RunTime.ToUniversalTime() was 5:33:00 PM and now.ToUniversalTime() was 5:32:03, so it satisfied both condition. Wouldn't it be simpler just to subtract (Runtime - now ).TotalSeconds < 60.0? – Anthony C Apr 21 '16 at 17:43
  • Ok, Yes Im seeing this now. Thanks – user3839756 Apr 21 '16 at 17:55
0

I like to use type TimeSpan when doing date-time comparisons.

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

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