Click here to Skip to main content
15,920,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys,

I'm having trouble about my program comparing the system time and the one I input.

My program has 4 variables which are:
startHr - Hour
startMin - Minute
endHr - Hour
endMin - Minute


my goal is that, when the system time's hour and minute falls in startHr and startMin it does something and when it reaches endHr and endMin it stops. I'm having trouble on how to code :( please help.
Posted
Updated 12-Mar-14 7:02am
v2
Comments
BillWoodruff 12-Mar-14 22:52pm    
What do you plan to do in order to check if the current time meets your criteria ? Use a Timer ?

You need to look at the type System.DateTime. It has operators '<', '<=', '>', '>=', and '=='. The result type (except Boolean in last case) is System.TimeSpan, from which you can find out any properties of the interval between two points in time. Note that this interval can be zero, positive or negative. Simple, isn't it? Please see:
http://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.timespan%28v=vs.110%29.aspx[^].

If you have some point of time, you can abstract out the date from them, so you could compare ignoring date (or ignoring time, compare just dates), this way:
http://msdn.microsoft.com/en-us/library/system.datetime.date(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.datetime.timeofday(v=vs.110).aspx[^].

—SA
 
Share this answer
 
v2
Build a pair of TimeSpan objects (startTime and endTime)
Then at some appropriate interval (using a Timer) check if DateTime.Now.TimeOfDay is between your limits.
[Edit: flesh it out and tune it up... No polling!!]
C#
using System;
using System.Threading;
    static void Main(string[] args)
    {
      TimeSpan testNow = DateTime.Now.TimeOfDay;  // in real use, this is the now TimeSpan value to use
      // interval all on same day
      testNow = new TimeSpan(12, 0, 0);
      TimesAreInputSoSetupProcessing(10, 15, 14, 30, testNow);   // now is in interval
      testNow = new TimeSpan(9, 0, 0);
      TimesAreInputSoSetupProcessing(10, 15, 14, 30, testNow);   // now is before interval
      testNow = new TimeSpan(20, 0, 0);
      TimesAreInputSoSetupProcessing(10, 15, 14, 30, testNow);   // now is after interval
      // intervals span midnight
      testNow = new TimeSpan(12, 0, 0);
      TimesAreInputSoSetupProcessing(21, 15, 13, 30, testNow);    // now is in interval
      testNow = new TimeSpan(18, 0, 0);
      TimesAreInputSoSetupProcessing(21, 15, 13, 30, testNow);    // now is outside interval
    }
    private static Timer beginTimer;
    private static Timer stopTimer;
    private static readonly TimeSpan OneDay = new TimeSpan(1, 0, 0, 0);  // this SHOULD be static. It'll never change.

    // call this when the user has input the 4 values (Hr and Min) of the two interval endpoints
    static void TimesAreInputSoSetupProcessing(int startHr, int startMin, int endHr, int endMin, TimeSpan now)
    {
      // I'm assuming you've already range-checked the 4 values before you get here!!!!
      // Or you'll add it here.
      TimeSpan startTime = new TimeSpan(startHr, startMin, 0);
      TimeSpan endTime = new TimeSpan(endHr, endMin, 0);
      TimeSpan untilBegin = startTime - now;
      TimeSpan untilStop = endTime - now;
        // check if start to end is all same day
      if (startTime < endTime)
      {
        if (startTime <= now && now < endTime)
        {
          // Already in the specified interval.
          untilBegin = TimeSpan.Zero;
        }
      }
      else if (now < endTime || startTime <= now) // spans midnight
      {
        // Already in the specified interval.
        untilBegin = TimeSpan.Zero;
      }

      // either end of the interval might be tomorrow!
      if (untilBegin < TimeSpan.Zero)
        untilBegin += OneDay;
      if (untilStop < TimeSpan.Zero)
        untilStop += OneDay;
      Console.WriteLine("startTime: {0}, endTime: {1}, now: {2}", startTime, endTime, now);
      Console.WriteLine("untilBegin: {0}", untilBegin);
      Console.WriteLine("untilStop: {0}", untilStop);
      //NOTE: the Timer creation is commented out for testing the untilXXX calculations
      //beginTimer = new Timer(BeginSomething, null, untilBegin, OneDay);
      //stopTimer = new Timer(StopSomething, null, untilStop, OneDay);
    }
    static void BeginSomething(object state)
    {
      // here is where you begin whatever processing you want to be done during the interval
    }
    static void StopSomething(object state)
    {
      // here is where you end the processing for today
    }

Note: these are all static because I was just using a Console App. They don't need to be static in the general case.
The Timers are setup to repeat every day.
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 12-Mar-14 13:21pm    
Sorry, this answer is so incomplete that it can easily confuse. (I did not vote this time.)
Please see my answer.
—SA
Matt T Heffron 12-Mar-14 15:47pm    
Better now?
Sergey Alexandrovich Kryukov 12-Mar-14 16:13pm    
Now, it's probably too much. :-) (Maybe that's why someone voted 1 :-) I voted 5 now...
—SA
Matt T Heffron 12-Mar-14 16:17pm    
Thanks
You know it just eats at you if you miss the edge cases... :laugh:
Sergey Alexandrovich Kryukov 12-Mar-14 16:19pm    
Exactly... :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900