현재 시간이 시간 범위에 속하는지 확인
.NET 3.5 사용
현재 시간이 시간 범위에 속하는지 확인하고 싶습니다.
지금까지 나는 currentime을 가지고있다 :
DateTime currentTime = new DateTime();
currentTime.TimeOfDay;
시간 범위를 변환하고 비교하는 방법을 비우고 있습니다. 이게 효과가 있을까요?
if (Convert.ToDateTime("11:59") <= currentTime.TimeOfDay
&& Convert.ToDateTime("13:01") >= currentTime.TimeOfDay)
{
//match found
}
UPDATE1 : 제안 해 주셔서 감사합니다. TimeSpan 함수에 익숙하지 않았습니다.
하루 중 시간을 확인하려면 다음을 사용하십시오.
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
}
절대 시간 사용 :
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
}
여기에 좋은 답변이 있지만 시작 시간이 종료 시간과 다른 날인 경우에는 해당되지 않습니다. 하루 경계를 넘어야하는 경우 다음과 같이 도움이 될 수 있습니다.
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
}
}
이 예에서 시간의 경계가 포함되어 있는지, 그리고이 여전히 사이에 24 시간 차이보다 적은 가정합니다 start
및 stop
.
if (new TimeSpan(11,59,0) <= currentTime.TimeOfDay && new TimeSpan(13,01,0) >= currentTime.TimeOfDay)
{
//match found
}
실제로 문자열을 TimeSpan으로 구문 분석하려면 다음을 사용할 수 있습니다.
TimeSpan start = TimeSpan.Parse("11:59");
TimeSpan end = TimeSpan.Parse("13:01");
이것에 대한 간단한 작은 확장 기능 :
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;
}
Try using the TimeRange object in C# to complete your goal.
TimeRange timeRange = new TimeRange();
timeRange = TimeRange.Parse("13:00-14:00");
bool IsNowInTheRange = timeRange.IsIn(DateTime.Now.TimeOfDay);
Console.Write(IsNowInTheRange);
Here is where I got that example of using TimeRange
The TimeOfDay
property returns a TimeSpan
value.
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
.
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.
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 ...
}
Using Linq we can simplify this by this
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))
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");
}
}
}
참고URL : https://stackoverflow.com/questions/1504494/find-if-current-time-falls-in-a-time-range
'development' 카테고리의 다른 글
nginx가 반환 한 서버 헤더를 어떻게 변경합니까? (0) | 2020.07.02 |
---|---|
장고 디버그 툴바가 표시되지 않음 (0) | 2020.07.02 |
git rebase --onto의 동작을 이해할 수 없습니다 (0) | 2020.07.02 |
iPhone 5 CSS 미디어 쿼리 (0) | 2020.07.02 |
Android : 갤러리에서로드 된 비트 맵이 ImageView에서 회전 됨 (0) | 2020.07.02 |