如何将系统时间与我在C#中输入的时间进行比较 [英] How Do I Compare The System Time With The Time I've Inputted In C#

查看:266
本文介绍了如何将系统时间与我在C#中输入的时间进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,



我在比较系统时间和输入的时间方面遇到麻烦。



我的程序有4个变量:

startHr - 小时

startMin - 分钟

endHr - 小时

endMin - 分钟




我的目标是,当系统时间的小时和分钟落在 startHr startMin 它做了什么,当它到达 endHr endMin 时它会停止。我在如何编码时遇到麻烦:(请帮助。

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.

推荐答案

你需要查看类型 System.DateTime 。它有运算符'<','< =','>','> ='和'=='。结果类型(除了最后一种情况下的布尔值)是 System.TimeSpan ,你可以从中找出两个时间点之间间隔的任何属性。注意这个间隔可以是零,正或负。简单,不是吗?请参阅:

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 [ ^ ]。



如果你有一些时间点,你可以抽出它们的日期,这样你就可以比较忽略日期(或忽略时间,只比较日期),这样:

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


建立一对 TimeSpan 对象(startTime和endTime)

然后在某个适当的间隔(使用Timer)检查 DateTime.Now.TimeOfDay 在你的限制之间。



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.

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
    }



注意:这些都是 static 因为我刚刚使用的是控制台应用程序。在一般情况下,它们不需要是静态的。

定时器设置为每天重复。


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.


这篇关于如何将系统时间与我在C#中输入的时间进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆