C# 如何检查当前时间是否在一个时间范围内?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/592248/
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
How can I check if the current time is between in a time frame?
提问by scottm
I have a service that user can configure to run during "off-peak" hours. They have the ability to set the time frame that the service can run.
我有一项用户可以配置为在“非高峰”时段运行的服务。他们能够设置服务可以运行的时间范围。
For Example:
例如:
User A works 8am-5pm, so they want to schedule the app to run between 5:30pm and 7:30am.
用户 A 的工作时间是上午 8 点到下午 5 点,因此他们希望将应用程序安排在下午 5:30 到早上 7:30 之间运行。
User B works 9pm-6am, so they schedule the app to run between 6:30am and 8:30 pm.
用户 B 的工作时间是晚上 9 点到早上 6 点,因此他们将应用安排在早上 6 点 30 分到晚上 8 点 30 分之间运行。
The point is that the app uses their computer while they are not.
关键是该应用程序使用他们的计算机,而他们不使用。
Given a DateTime of the current time, a DateTime of the start and a DateTime of the stop time, how can I check if current is between start and stop.
给定当前时间的 DateTime、开始的 DateTime 和停止时间的 DateTime,如何检查当前是否在开始和停止之间。
The tricky part for me is that the time can cross the midnight boundary.
对我来说棘手的部分是时间可以跨越午夜边界。
采纳答案by Daniel LeCheminant
If startTime
and endTime
represent a single time interval (it will only happen once, and startTime
and endTime
represent the date and the time to start/stop), then it's as easy as saying
如果startTime
和endTime
表示单个时间间隔(它只会发生一次,并startTime
和endTime
代表的日期和开始时间/停止),那么它的那么容易,因为说
bool isTimeBetween = someTime >= startTime && someTime <= endTime;
If it's a recurring event (happens every day, during some interval), you can do comparisons using the TimeOfDay
property. (The recurring case is the one where you have to consider a start/stop that crosses midnight)
如果它是一个重复事件(每天发生,在某个时间间隔内),您可以使用该TimeOfDay
属性进行比较。(反复出现的情况是您必须考虑跨越午夜的开始/停止)
static public bool IsTimeOfDayBetween(DateTime time,
TimeSpan startTime, TimeSpan endTime)
{
if (endTime == startTime)
{
return true;
}
else if (endTime < startTime)
{
return time.TimeOfDay <= endTime ||
time.TimeOfDay >= startTime;
}
else
{
return time.TimeOfDay >= startTime &&
time.TimeOfDay <= endTime;
}
}
(Note: This code assumes that if start == end
, then it covers all times. You made a comment to this effect on another post)
(注意:此代码假设 if start == end
,那么它涵盖所有时间。您在另一篇文章中对此效果发表了评论)
For example, to check if it's between 5 AM and 9:30 PM
例如,要检查它是否在上午 5 点到晚上 9:30 之间
IsTimeOfDayBetween(someTime, new TimeSpan(5, 0, 0), new TimeSpan(21, 30, 0))
If startTime
and endTime
are DateTime
s, you could say
如果startTime
和endTime
是DateTime
s,你可以说
IsTimeOfDayBetween(someTime, startTime.TimeOfDay, endTime.TimeOfDay)
回答by Ron Savage
I'm assuming you are saving the start and end times in the applications configuration file, so all you basically have to do is have your application set a flag to "on" when the "start time" occurs and set it to "off" when the stop time occurs.
我假设您将开始时间和结束时间保存在应用程序配置文件中,因此您基本上要做的就是让您的应用程序在“开始时间”发生时将标志设置为“on”并将其设置为“off”当停止时间发生时。
That way you don't have to be constantly checking if "now" is "between start and end" times.
这样你就不必经常检查“现在”是否在“开始和结束之间”时间。
回答by Tim
if (current >= start && current <= stop)
if (current >= start && current <= stop)
(or without = )
(或没有 = )
I think that is all you need?
我想这就是你所需要的吗?
the midnight boundary is a red herring - all you need to know is the status of both of the comparisons.
午夜边界是一个红鲱鱼 - 您需要知道的只是两个比较的状态。
If you do things like handle weekends differently then you have other logic, but the basic compare is simple.
如果您以不同的方式处理周末之类的事情,那么您就有其他逻辑,但基本的比较很简单。
回答by MSN
So I assume from the question that you want to know if given a start time and end time for a day (not including the actual date, i.e., 1/1/1900 or something like that) to see if another time is with the time specified by start and end. E.g., if start is 9pm and end is 9am, accept 10pm but reject 10am.
因此,我从问题中假设您想知道是否给定了一天的开始时间和结束时间(不包括实际日期,即 1/1/1900 或类似的日期),以查看其他时间是否与时间一致由开始和结束指定。例如,如果开始是晚上 9 点,结束是上午 9 点,则接受晚上 10 点但拒绝上午 10 点。
You can do this either per time range types (times are equal, end is after start, end is before start) which is simple:
您可以按时间范围类型(时间相等,结束在开始之后,结束在开始之前)执行此操作,这很简单:
if (end==start) return true
else if (end>start) return start<=time && time<=end
else return !(time>end && time<start)
Or you can extend the range of start and end such that end is always after start as such:
或者您可以扩展 start 和 end 的范围,使 end 始终在 start 之后:
if (end<=start) end += <24 hours>
if (time<start) time+= <24 hours>
return time<=end
回答by bharatwaj vanamamalai
DateTime t1;
t1 = DateTime.Now;
// loop inbetween start and end time
if (t1>=start_time &&t1<=end_time)
{
//your code / action
}
//if you are using sql to get values
start_time = Convert.ToDateTime(row.Cells[10].Text);
end_time = Convert.ToDateTime(row.Cells[11].Text);
//convert them to string or you will get some error!!!