C#比较两个时间间隔之间的时间

时间:2012-05-17 06:42:54

标签: c# c#-4.0 datetime

尝试比较两次之间的给定时间,看它是否在这些间隔内。例如如果给定时间是00:00我需要知道它是否在21:00:00到7:00:00之间。尝试TimeSpan.Compare没有锁,也使用了>或者<对于时间部分。

e.g。 给定间隔时间:

7:00:00 to 19:00:00
19:00:00 to 21:00:00
21:00:00 to 7:00:00

比较时间:

  

00:00:00和01:00:00

任何帮助将不胜感激。

更新了问题:

看起来要求很安静含糊。要求基本上是通过时间(TimeSpan)并与两个TimeSpan间隔进行比较,以查看它们是否落入这些间隔。

e.g。让我们说如果员工在下面的不同时段工作,他们会获得不同的津贴:

  

日期范围:2012-01-01至2012-31

19:00:00 to 21:00:00 ($10.00)
21:00:00 to 7:00:00 ($11.00)
7:00:00 to 19:00:00 ($12.00)

要计算员工的小时费率,我需要检查员工是否工作

  
      
  1. 日期范围:2012-01-01至2012-31
  2.   
  3. 在上述时间范围之间。
  4.   

并相应地应用$ Rate。

6 个答案:

答案 0 :(得分:8)

你可以自己写一个扩展方法,如;

public static class TimeExtensions
{
    public static bool IsBetween(this DateTime time, DateTime startTime, DateTime endTime)
    {
        if (time.TimeOfDay == startTime.TimeOfDay) return true;
        if (time.TimeOfDay == endTime.TimeOfDay) return true;

        if (startTime.TimeOfDay <= endTime.TimeOfDay)
            return (time.TimeOfDay >= startTime.TimeOfDay && time.TimeOfDay <= endTime.TimeOfDay);
        else
            return !(time.TimeOfDay >= endTime.TimeOfDay && time.TimeOfDay <= startTime.TimeOfDay);
    }
}

答案 1 :(得分:2)

以下代码......

static class DateTimeExt {

    public static bool TimeOfDayIsBetween(this DateTime t, DateTime start, DateTime end) {

        var time_of_day = t.TimeOfDay;
        var start_time_of_day = start.TimeOfDay;
        var end_time_of_day = end.TimeOfDay;

        if (start_time_of_day <= end_time_of_day)
            return start_time_of_day <= time_of_day && time_of_day <= end_time_of_day;

        return start_time_of_day <= time_of_day || time_of_day <= end_time_of_day;

    }

}

class Program {

    static void Test(DateTime t, DateTime start, DateTime end) {
        bool falls_within = t.TimeOfDayIsBetween(start, end);
        Console.WriteLine("{0} \t[{1},\t{2}]:\t{3}", t, start, end, falls_within);
    }

    static void Main(string[] args) {

        Test(new DateTime(2012, 1, 1, 0, 0, 0), new DateTime(2012, 1, 1, 7, 0, 0), new DateTime(2012, 1, 1, 19, 0, 0));
        Test(new DateTime(2012, 1, 1, 1, 0, 0), new DateTime(2012, 1, 1, 7, 0, 0), new DateTime(2012, 1, 1, 19, 0, 0));

        Test(new DateTime(2012, 1, 1, 0, 0, 0), new DateTime(2012, 1, 1, 19, 0, 0), new DateTime(2012, 1, 1, 21, 0, 0));
        Test(new DateTime(2012, 1, 1, 1, 0, 0), new DateTime(2012, 1, 1, 19, 0, 0), new DateTime(2012, 1, 1, 21, 0, 0));

        Test(new DateTime(2012, 1, 1, 0, 0, 0), new DateTime(2012, 1, 1, 21, 0, 0), new DateTime(2012, 1, 1, 7, 0, 0));
        Test(new DateTime(2012, 1, 1, 1, 0, 0), new DateTime(2012, 1, 1, 21, 0, 0), new DateTime(2012, 1, 1, 7, 0, 0));

        Test(new DateTime(2012, 05, 17, 00, 00, 00, 00), new DateTime(2012, 05, 17, 20, 00, 00), new DateTime(2012, 05, 18, 08, 00, 00));
        Test(new DateTime(2012, 05, 17, 09, 00, 00, 00), new DateTime(2012, 05, 17, 20, 00, 00), new DateTime(2012, 05, 18, 08, 00, 00));

        Test(new DateTime(2012, 1, 1, 0, 0, 0), new DateTime(2012, 1, 1, 0, 0, 0), new DateTime(2012, 1, 1, 0, 0, 0));

    }

}

...打印以下结果:

1/1/2012 12:00:00 AM    [1/1/2012 7:00:00 AM,   1/1/2012 7:00:00 PM]:   False
1/1/2012 1:00:00 AM     [1/1/2012 7:00:00 AM,   1/1/2012 7:00:00 PM]:   False
1/1/2012 12:00:00 AM    [1/1/2012 7:00:00 PM,   1/1/2012 9:00:00 PM]:   False
1/1/2012 1:00:00 AM     [1/1/2012 7:00:00 PM,   1/1/2012 9:00:00 PM]:   False
1/1/2012 12:00:00 AM    [1/1/2012 9:00:00 PM,   1/1/2012 7:00:00 AM]:   True
1/1/2012 1:00:00 AM     [1/1/2012 9:00:00 PM,   1/1/2012 7:00:00 AM]:   True
5/17/2012 12:00:00 AM   [5/17/2012 8:00:00 PM,  5/18/2012 8:00:00 AM]:  True
5/17/2012 9:00:00 AM    [5/17/2012 8:00:00 PM,  5/18/2012 8:00:00 AM]:  False
1/1/2012 12:00:00 AM    [1/1/2012 12:00:00 AM,  1/1/2012 12:00:00 AM]:  True

答案 2 :(得分:1)

或者,如果您的需求超出此范围,请使用我的favorites libraries

答案 3 :(得分:0)

var time1 = DateTime.Now.TimeOfDay;
var time2 = DateTime.Now.AddDays(1.3).TimeOfDay;
var diff = time2 - time1;

因此,这仅仅是为了表明添加1.3天仍会给出相同的时间答案。

答案 4 :(得分:0)

不确定为什么时间跨度不适合你。

我在POC应用程序中尝试了这个示例,但它确实有效。

 DateTime t1 = DateTime.Now;
    DateTime t2 = DateTime.UtcNow;
    t1.TimeOfDay.CompareTo(t2.TimeOfDay);

试着希望它能解决问题。

答案 5 :(得分:0)

string dt=DateTime.Now.ToShortTimeString();
DateTime presenttime=Convert.ToDateTime(dt);

starttime = starttimepicker.ValueString;
DateTime dtime=Convert.ToDateTime(starttime);

if (dtime > presenttime)
{
   MessageBox.Show("Time cannot be greater than System Time. Please Try Again!", "Do not selecting future time", MessageBoxButton.OK);
            starttimepicker.Value = presenttime;
}