3

尝试比较两次之间的给定时间以查看它是否在这些时间间隔内。例如,如果给定时间是 00:00,我需要确定它是否介于 21:00:00 到 7:00:00 之间。试过 TimeSpan.Compare 没有锁,也用 > 或 < 表示时间部分。

例如给定间隔:

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 间隔进行比较,以查看它们是否属于这些间隔。

例如,假设员工在以下不同时间段工作,他们将获得不同的津贴:

日期范围: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. 上述时间范围之间。

并相应地应用 $ Rate。

4

6 回答 6

11

您可以为自己编写一个扩展方法,例如;

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);
    }
}
于 2012-05-17T06:46:32.990 回答
3

以下代码...

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
于 2012-05-17T12:27:13.057 回答
1

或者,如果您的需求超出此范围,请使用我最喜欢的库之一。

于 2012-05-17T06:48:55.033 回答
0
var time1 = DateTime.Now.TimeOfDay;
var time2 = DateTime.Now.AddDays(1.3).TimeOfDay;
var diff = time2 - time1;

因此,这只是举例说明添加 1.3 天仍然给出相同时间的答案。

于 2012-05-17T06:49:09.363 回答
0

不知道为什么时间跨度不适合你。

我在我的 POC 应用程序中尝试了这个示例并且它有效。

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

试试这个希望它能解决问题。

于 2012-05-17T06:50:15.073 回答
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;
}
于 2013-11-27T06:03:44.640 回答