How to Know If a Datetime Is Between a Daterange in C#

How to know if a DateTime is between a DateRange in C#

Nope, doing a simple comparison looks good to me:

return dateToCheck >= startDate && dateToCheck < endDate;

Things to think about though:

  • DateTime is a somewhat odd type in terms of time zones. It could be UTC, it could be "local", it could be ambiguous. Make sure you're comparing apples with apples, as it were.
  • Consider whether your start and end points should be inclusive or exclusive. I've made the code above treat it as an inclusive lower bound and an exclusive upper bound.

Way to check if a DateTime is between two Dates in C#

Here you are:

DateTime from = new DateTime(1960,1,1);
DateTime to = new DateTime(1990, 12, 31);
DateTime input = DateTime.Now;
Console.WriteLine(from <= input && input <= to); // False
input = new DateTime(1960,1,1);
Console.WriteLine(from <= input && input <= to); // True

Hope this help.

Checking if date is within a date range

you are asking the date to be >= 5-days-time and <= 5-days-time. So unless it == 5-days-time it'll return false. I think you mean this:

DateTime dt = new DateTime();
if (DateTime.TryParse(c.Text, out dt))
{
DateTime now = DateTime.Now;
if (dt.Date >= now.Date && dt.Date <= now.AddDays(Mod.ValidUntilDays).Date)
{
e.Item.Style.Add("background-color", "#FF0303");
}
}

Check if datetime instance falls in between other two datetime objects

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
}

Check if current DateTime is between certain percentage days - datetime ranges

try this

DateTime today = Convert.ToDateTime("20.02.2018");
DateTime start = Convert.ToDateTime("01.01.2018");
DateTime End = Convert.ToDateTime("01.04.2018");
TimeSpan diff = (End - start);
double nrofdays = diff.TotalDays;
double percentage = (((today) - (start)).TotalDays) / nrofdays * 100;
if (percentage>=40 && percentage <= 60)
{
MessageBox.Show("yes my date 20.02.2018 is between 40% and 60%");
}

c# check if daterange between an interval

Store your date ranges as pairs as in start to end, where end would next start - 1 day.

Makes things much clearer in the code.

How to check if a DateTime range is within another 3 month DateTime range

You would call IntervalInQuarters as follows:

IntervalInQuarters(new DateTime(2007, 10, 10), new DateTime(2009, 10, 11));

The function returns a list of quarter start dates. Note that the range of quarters searched is defined within the function itself. Please edit as appropriate for your situation. They key point is to make sure the interval/quarter intersection logic is right.

private List<DateTime> IntervalInQuarters(DateTime myStartDate, DateTime myEndDate)
{
DateTime quarterStart = new DateTime(2006, 06, 01);
DateTime nextQuarterStart = new DateTime(2006, 09, 01);
DateTime finalDate = new DateTime(2011, 01, 01);
List<DateTime> foundQuarters = new List<DateTime>();

while (quarterStart < finalDate)
{
// quarter intersects interval if:
// its start/end date is within our interval
// our start/end date is within quarter interval
DateTime quarterEnd = nextQuarterStart.AddDays(-1);
if (DateInInterval(myStartDate, quarterStart, quarterEnd) ||
DateInInterval(myEndDate, quarterStart, quarterEnd) ||
DateInInterval(quarterStart, myStartDate, myEndDate) ||
DateInInterval(quarterEnd, myStartDate, myEndDate))
{
foundQuarters.Add(quarterStart);
}

quarterStart = nextQuarterStart;
nextQuarterStart = nextQuarterStart.AddMonths(3);
}

return foundQuarters;
}

private bool DateInInterval(DateTime myDate, DateTime intStart, DateTime intEnd)
{
return ((intStart <= myDate) && (myDate <= intEnd));
}

Check if a date range is within a date range

Basically, a date range overlaps another if any of its endings are within the other range, or vice versa.

static bool AllowedToAdd(List<Membership> membershipList, Membership newItem)
{
return !membershipList.Any(m =>
(m.StartDate < newItem.StartDate &&
newItem.StartDate < (m.EndDate ?? DateTime.MaxValue))
||
(m.StartDate < (newItem.EndDate ?? DateTime.MaxValue) &&
(newItem.EndDate ?? DateTime.MaxValue) <= (m.EndDate ?? DateTime.MaxValue))
||
(newItem.StartDate < m.StartDate &&
m.StartDate < (newItem.EndDate ?? DateTime.MaxValue))
||
(newItem.StartDate < (m.EndDate ?? DateTime.MaxValue) &&
(m.EndDate ?? DateTime.MaxValue) <= (newItem.EndDate ?? DateTime.MaxValue))
);
}

With the usage:

if (AllowedToAdd(membershipList, newItem))
membershipList.Add(newItem);


Related Topics



Leave a reply



Submit