Using .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.
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 ...
}
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);
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
.
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
.
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");
}
}
}
A simple little extension function for this:
public static bool IsBetween(this DateTime now, TimeSpan start, TimeSpan end)
{
var time = now.TimeOfDay;
// Scenario 1: If the start time and the end time are in the same day.
if (start <= end)
return time >= start && time <= end;
// Scenario 2: The start time and end time is on different days.
return time >= start || time <= end;
}
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))
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.
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 start = TimeSpan.Parse("11:59");
TimeSpan end = TimeSpan.Parse("13:01");
Source: Stackoverflow.com