I would like to know a simple algorithm to check if the given instance of datetime
lies between another two instances in C#
.
Note:
I skimmed though this How do I check if a given datetime object is "between" two datetimes? and it was for python and many more for php. Most of the other questions were regarding difference between the two.
Details:
I am more specific about the time, date does not matter to me. For example i got DataBase
entry for a staff who works between 10:00
Am - 9:00
Pm and me as principal[for example] would like to know which staff is engaged in class at the given time like 2:00 Pm
. Now this would return me the staff's details who are engaged at this time.
DateTime.Ticks will account for the time. Use .Ticks on the DateTime to convert your dates into longs. Then just use a simple if stmt to see if your target date falls between.
// Assuming you know d2 > d1
if (targetDt.Ticks > d1.Ticks && targetDt.Ticks < d2.Ticks)
{
// targetDt is in between d1 and d2
}
Do simple compare > and <.
if (dateA>dateB && dateA<dateC)
//do something
If you care only on time:
if (dateA.TimeOfDay>dateB.TimeOfDay && dateA.TimeOfDay<dateC.TimeOfDay)
//do something
You can use:
if ((DateTime.Compare(dateToCompare, dateIn) == 1) && (DateTime.Compare(dateToCompare, dateOut) == 1)
{
//do code here
}
or
if ((dateToCompare.CompareTo(dateIn) == 1) && (dateToCompare.CompareTo(dateOut) == 1))
{
//do code here
}
Write yourself a Helper function:
public static bool IsBewteenTwoDates(this DateTime dt, DateTime start, DateTime end)
{
return dt >= start && dt <= end;
}
Then call: .IsBewteenTwoDates(DateTime.Today ,new DateTime(,,));
This will help for sure.
public static int year1, year2, year3, month1, month2, month3, day1, day2, day3;
public static string dateA, dateB, dateC;
static bool iswithindaterange(string dateA, string dateB, string dateC)
{
month1 = Convert.ToInt32((dateA.Split('/'))[0]); // Splits the value of the string on the '/' into month , day and year
day1 = Convert.ToInt32((dateA.Split('/'))[1]);
year1 = Convert.ToInt32((dateA.Split('/'))[2]);
month2 = Convert.ToInt32((dateB.Split('/'))[0]);
day2 = Convert.ToInt32((dateB.Split('/'))[1]);
year2 = Convert.ToInt32((dateB.Split('/'))[2]);
month3 = Convert.ToInt32((dateC.Split('/'))[0]);
day3 = Convert.ToInt32((dateC.Split('/'))[1]);
year3 = Convert.ToInt32((dateC.Split('/'))[2]);
DateTime startdate = new DateTime(year1, month1, day1);
DateTime enddate = new DateTime(year2, month2, day2);
DateTime checkdate = new DateTime(year3, month3, day3);
if (checkdate >= startdate && checkdate <= enddate)
{
return true;
}
else
{
return false;
}
}
static void Main(string[] args)
{
dateA = "1/22/2016"; // Date Format (MM/dd/YYYY)
dateB = "9/20/2016";
dateC = "5/18/2016";
bool answer;
answer = iswithindaterange(dateA, dateB, dateC);
if (answer == true)
{
Console.WriteLine("True");
}
else
{
Console.WriteLine("False");
}
}
来源:https://stackoverflow.com/questions/5672862/check-if-datetime-instance-falls-in-between-other-two-datetime-objects