Check If Datetime Instance Falls in Between Other Two Datetime Objects

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
}

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.

How do I check if a given datetime object is between two datetimes?

Your answer is the way to go as long as start_time and end_time don't have an associated tzinfo class. You can't directly compare a naive datetime with a timezoned-datetime.

How to check if DateTime.Now is between two given DateTimes for time part only?

First you need to convert everything to the same units (we'll use TimeSpan), then you need to see whether the start-end times cross midnight, and finally do your comparison based on the results of that check:

// convert everything to TimeSpan
TimeSpan start = new TimeSpan(22, 0, 0);
TimeSpan end = new TimeSpan(07, 0, 0);
TimeSpan now = DateTime.Now.TimeOfDay;
// see if start comes before end
if (start < end)
return start <= now && now <= end;
// start is after end, so do the inverse comparison
return !(end < now && now < start);

Here's a function to do it for you:

bool TimeBetween(DateTime datetime, TimeSpan start, TimeSpan end)
{
// convert datetime to a TimeSpan
TimeSpan now = datetime.TimeOfDay;
// see if start comes before end
if (start < end)
return start <= now && now <= end;
// start is after end, so do the inverse comparison
return !(end < now && now < start);
}

You would call it like:

bool silenceAlarm = TimeBetween(DateTime.Now, StartTime.Value, EndTime.Value);

How to check a date exists in between two dates?

The following shows an example using DateTime:

var startDate = new DateTime(2014,11,10);
var endDate = new DateTime(2014,11,15);

var dateToCheck = new DateTime(2014,11,12);

if(startDate < dateToCheck && endDate > dateToCheck)
{
// Do something
}

How to tell if a date is between two other dates?

As you are still not satisfied, I have another answer for you. Without using datetime and year.

It just uses built-in tuples and comparing them:

d1 = (3, 28)
d2 = (3, 31)
d3 = (4, 2)
if d1 < d2 < d3:
print("BETWEEN!")
else:
print("NOT!")

You can create tuple like these easily:

day = 16
month = 4
d = (month, day)

Determine Whether Two Date Ranges Overlap

(StartA <= EndB) and (EndA >= StartB)

Proof:

Let ConditionA Mean that DateRange A Completely After DateRange B

_                        |---- DateRange A ------|
|---Date Range B -----| _

(True if StartA > EndB)

Let ConditionB Mean that DateRange A is Completely Before DateRange B

|---- DateRange A -----|                        _ 
_ |---Date Range B ----|

(True if EndA < StartB)

Then Overlap exists if Neither A Nor B is true -

(If one range is neither completely after the other,

nor completely before the other,
then they must overlap.)

Now one of De Morgan's laws says that:

Not (A Or B) <=> Not A And Not B

Which translates to: (StartA <= EndB) and (EndA >= StartB)


NOTE: This includes conditions where the edges overlap exactly. If you wish to exclude that,

change the >= operators to >, and <= to <


NOTE2. Thanks to @Baodad, see this blog, the actual overlap is least of:

{ endA-startA, endA - startB, endB-startA, endB - startB }

(StartA <= EndB) and (EndA >= StartB)
(StartA <= EndB) and (StartB <= EndA)


NOTE3. Thanks to @tomosius, a shorter version reads:

DateRangesOverlap = max(start1, start2) < min(end1, end2)

This is actually a syntactical shortcut for what is a longer implementation, which includes extra checks to verify that the start dates are on or before the endDates. Deriving this from above:

If start and end dates can be out of order, i.e., if it is possible that startA > endA or startB > endB, then you also have to check that they are in order, so that means you have to add two additional validity rules:

(StartA <= EndB) and (StartB <= EndA) and (StartA <= EndA) and (StartB <= EndB)
or:

(StartA <= EndB) and (StartA <= EndA) and (StartB <= EndA) and (StartB <= EndB)
or,

(StartA <= Min(EndA, EndB) and (StartB <= Min(EndA, EndB))
or:

(Max(StartA, StartB) <= Min(EndA, EndB)

But to implement Min() and Max(), you have to code, (using C ternary for terseness),:

((StartA > StartB) ? StartA : StartB) <= ((EndA < EndB) ? EndA : EndB)



Related Topics



Leave a reply



Submit