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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10631044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:35:08  来源:igfitidea点击:

C# Compare Time between Two Time Intervals

c#c#-4.0datetime

提问by Nil Pun

Trying to compare a given Time between two times to see if it's within the those intervals. e.g. if given Time is 00:00 I need to find out if it falls between 21:00:00 to 7:00:00. Tried TimeSpan.Compare no lock and also used > or < for Time Part.

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

e.g. Given Intervals:

例如给定间隔:

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

Times to compare:

比较时间:

00:00:00 and 01:00:00

00:00:00 和 01:00:00

Any help will be appreciated.

任何帮助将不胜感激。

Updated Question:

更新的问题:

Looks like the requirement is quiet vague. The requirement is basically to pass the Time (TimeSpan) and compare with two TimeSpan intervals to see if they fall in to the those interval.

看起来要求是安静的模糊。要求基本上是通过时间(TimeSpan)并与两个TimeSpan间隔进行比较,看看它们是否落在那些间隔内。

e.g. Lets say employees get different allowances if they work on different time slots below:

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

Date Range: 2012-01-01 to 2012-31

19:00:00 to 21:00:00 (.00)
21:00:00 to 7:00:00 (.00)
7:00:00 to 19:00:00 (.00)

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

19:00:00 to 21:00:00 (.00)
21:00:00 to 7:00:00 (.00)
7:00:00 to 19:00:00 (.00)

To calculate the hourly rate for an employee I need to check whether the employee has worked

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

  1. Between Date Range :2012-01-01 to 2012-31
  2. Between Time Range above.
  1. 日期范围:2012-01-01 至 2012-31
  2. 在上面的时间范围之间。

And apply $ Rate accordingly.

并相应地应用 $ Rate。

采纳答案by RJ Lohan

You could write youself an extension method like;

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

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);
    }
}

回答by David Brabant

Or, if your needs go beyond that, use one of my favorites libraries.

或者,如果您的需求不止于此,请使用我最喜欢的库之一

回答by Ian

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

So this is just for example perposes to show that adding 1.3 days still gives the same time answer.

所以这只是为了表明增加 1.3 天仍然给出相同的时间答案。

回答by Shailesh

Not sure why timespan is not working for you.

不知道为什么时间跨度对你不起作用。

I tried this sample in my POC application and it worked.

我在我的 POC 应用程序中尝试了这个示例,并且成功了。

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

Try this hope it will solve the problem.

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

回答by Branko Dimitrijevic

The following code...

下面的代码...

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

    }

}

...prints the following result:

...打印以下结果:

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

回答by Priyank shah

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