C# 查找当前时间是否在时间范围内
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1504494/
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
Find if current time falls in a time range
提问by John M
Using .NET 3.5
使用 .NET 3.5
I want to determine if the current time falls in a time range.
我想确定当前时间是否在一个时间范围内。
So far I have the currentime:
到目前为止,我有当前时间:
DateTime currentTime = new DateTime();
currentTime.TimeOfDay;
I'm blanking out on how to get the time range converted and compared. Would this work?
我正在研究如何转换和比较时间范围。这行得通吗?
if (Convert.ToDateTime("11:59") <= currentTime.TimeOfDay
&& Convert.ToDateTime("13:01") >= currentTime.TimeOfDay)
{
//match found
}
UPDATE1: Thanks everyone for your suggestions. I wasn't familiar with the TimeSpan function.
UPDATE1:感谢大家的建议。我不熟悉 TimeSpan 函数。
采纳答案by Frank Bollack
For checking for a time of day use:
要检查一天中的某个时间,请使用:
TimeSpan start = new TimeSpan(10, 0, 0); //10 o'clock
TimeSpan end = new TimeSpan(12, 0, 0); //12 o'clock
TimeSpan now = DateTime.Now.TimeOfDay;
if ((now > start) && (now < end))
{
//match found
}
For absolute times use:
对于绝对时间使用:
DateTime start = new DateTime(2009, 12, 9, 10, 0, 0)); //10 o'clock
DateTime end = new DateTime(2009, 12, 10, 12, 0, 0)); //12 o'clock
DateTime now = DateTime.Now;
if ((now > start) && (now < end))
{
//match found
}
回答by JDunkerley
if (new TimeSpan(11,59,0) <= currentTime.TimeOfDay && new TimeSpan(13,01,0) >= currentTime.TimeOfDay)
{
//match found
}
if you really want to parse a string into a TimeSpan, then you can use:
如果您真的想将字符串解析为 TimeSpan,则可以使用:
TimeSpan start = TimeSpan.Parse("11:59");
TimeSpan end = TimeSpan.Parse("13:01");
回答by Michael La Voie
You're very close, the problem is you're comparing a DateTime to a TimeOfDay. What you need to do is add the .TimeOfDay property to the end of your Convert.ToDateTime() functions.
您非常接近,问题是您将 DateTime 与 TimeOfDay 进行比较。您需要做的是将 .TimeOfDay 属性添加到 Convert.ToDateTime() 函数的末尾。
回答by SLaks
The TimeOfDay
propertyreturns a TimeSpan
value.
该TimeOfDay
属性返回一个TimeSpan
值。
Try the following code:
试试下面的代码:
TimeSpan time = DateTime.Now.TimeOfDay;
if (time > new TimeSpan(11, 59, 00) //Hours, Minutes, Seconds
&& time < new TimeSpan(13, 01, 00)) {
//match found
}
Also, new DateTime()
is the same as DateTime.MinValue
and will always be equal to 1/1/0001 12:00:00 AM
. (Value types cannot have non-empty default values) You want to use DateTime.Now
.
此外,new DateTime()
与 相同DateTime.MinValue
并且将始终等于1/1/0001 12:00:00 AM
。(值类型不能有非空的默认值)你想使用DateTime.Now
.
回答by stewsha
Try using the TimeRange object in C# to complete your goal.
尝试使用 C# 中的 TimeRange 对象来完成您的目标。
TimeRange timeRange = new TimeRange();
timeRange = TimeRange.Parse("13:00-14:00");
bool IsNowInTheRange = timeRange.IsIn(DateTime.Now.TimeOfDay);
Console.Write(IsNowInTheRange);
回答by Nick
Some good answers here but none cover the case of your start time being in a different day than your end time. If you need to straddle the day boundary, then something like this may help:
这里有一些很好的答案,但没有一个涵盖您的开始时间与结束时间不同的情况。如果您需要跨越一天的界限,那么这样的事情可能会有所帮助:
TimeSpan start = TimeSpan.Parse("22:00"); // 10 PM
TimeSpan end = TimeSpan.Parse("02:00"); // 2 AM
TimeSpan now = DateTime.Now.TimeOfDay;
if (start <= end)
{
// start and stop times are in the same day
if (now >= start && now <= end)
{
// current time is between start and stop
}
}
else
{
// start and stop times are in different days
if (now >= start || now <= end)
{
// current time is between start and stop
}
}
Note that in this example the time boundaries are inclusive and that this still assumes less than a 24-hour difference between start
and stop
.
请注意,在此示例中,时间边界包含在内,并且仍然假定start
和之间的差异小于 24 小时stop
。
回答by Edu Cielo
Using Linq we can simplify this by this
使用 Linq 我们可以通过这个简化这个
Enumerable.Range(0, (int)(to - from).TotalHours + 1)
.Select(i => from.AddHours(i)).Where(date => date.TimeOfDay >= new TimeSpan(8, 0, 0) && date.TimeOfDay <= new TimeSpan(18, 0, 0))
回答by Elliott
Will this be simpler for handling the day boundary case? :)
这对于处理日边界情况会更简单吗?:)
TimeSpan start = TimeSpan.Parse("22:00"); // 10 PM
TimeSpan end = TimeSpan.Parse("02:00"); // 2 AM
TimeSpan now = DateTime.Now.TimeOfDay;
bool bMatched = now.TimeOfDay >= start.TimeOfDay &&
now.TimeOfDay < end.TimeOfDay;
// Handle the boundary case of switching the day across mid-night
if (end < start)
bMatched = !bMatched;
if(bMatched)
{
// match found, current time is between start and end
}
else
{
// otherwise ...
}
回答by André Snede Kock
A simple little extension function for this:
一个简单的小扩展功能:
public static bool IsBetween(this DateTime now, TimeSpan start, TimeSpan end)
{
var time = now.TimeOfDay;
// If the start time and the end time is in the same day.
if (start <= end)
return time >= start && time <= end;
// The start time and end time is on different days.
return time >= start || time <= end;
}
回答by Patel Vishal
using System;
public class Program
{
public static void Main()
{
TimeSpan t=new TimeSpan(20,00,00);//Time to check
TimeSpan start = new TimeSpan(20, 0, 0); //8 o'clock evening
TimeSpan end = new TimeSpan(08, 0, 0); //8 o'clock Morning
if ((start>=end && (t<end ||t>=start))||(start<end && (t>=start && t<end)))
{
Console.WriteLine("Mached");
}
else
{
Console.WriteLine("Not Mached");
}
}
}