0

I am searching for a way to find out if a period of time is between 2 DateTimes. For example, In a List I have period of time(start and end) time in an object. Like

Start: 16:00pm, End: 17.00pm

I have a Form which allows the User to add a period of time to this existing list. For example, in the form user can enter following:

Start : 16:30pm , End: 17:40pm

How can I check foreach Object in my List, if user entered time period doesn't already exist?

In this example user input 16:30 is between 16:00 and 17:00 and this should not be allowed. How can I do this?

Please do let me know it problem needs more explanation.

NCCSBIM071
  • 1,207
  • 1
  • 16
  • 30
  • Check this out: [Time period library for .Net](https://www.codeproject.com/Articles/168662/Time-Period-Library-for-NET) – Maciej Los Feb 03 '21 at 09:26
  • I am not allowed to use external librarys :( But if i dont understand this article wrong it doesnt exist a method for my problem solving in .net – Invictus Warrior Feb 03 '21 at 10:00
  • @InvictusWarrior you don't need an entire library, just two simple comparisons: `a.start < b.end && b.start < a.end`. As for `I am not allowed to use external librarys` you can't use .NET without "external" libraries, period. Even System libraries are published as NuGet packages. Every time you compile your project it restores quite a lot of packages from NuGet – Panagiotis Kanavos Feb 03 '21 at 10:05

1 Answers1

-1
bool IsWithin(DateTime firstStart, DateTime firstEnd, DateTime secondStart, DateTime secondEnd) {
    long fs = firstStart.ToFileTime();
    long fe = firstEnd.ToFiletime();
    long ss = secondStart.ToFileTime();
    long se = secondEnd.ToFileTime();
    return fs > ss && fe < se;
}

This method takes the four dates and returns if the first period is within the second period. Then u just check if this method returns falls for all objects. Hope that this helps you :)

  • 2
    Why use `ToFileTime` instead of comparing the values directly? – Panagiotis Kanavos Feb 03 '21 at 09:39
  • 1
    The only thing this does is change the tick count base from `0001-01-01` to `1601-01-01`. Given how fast comparisons are, `firstStart.ToFileTime()` alone is more expensive than `firsttStart > secondStart`. A DateTime stores a tick count internally and [the comparison operators](https://referencesource.microsoft.com/#mscorlib/system/datetime.cs,1515) compare the tick values – Panagiotis Kanavos Feb 03 '21 at 09:45
  • Doesnt work everytime, Fail for example : First:09:00-10:00 Second: 09:02-10:01 Your Method say that it doesnt is in this period of time but this is wrong :( – Invictus Warrior Feb 03 '21 at 09:55
  • @InvictusWarrior that's because you want to find if two ranges *overlap* while this method tells you if one range is *fully* inside the other. – Panagiotis Kanavos Feb 03 '21 at 10:00
  • return (firstStart > secondStart && firstStart < secondEnd) || (firstEnd > secondStart && firstEnd < secondEnd) This should resolve your problem – Philipp Kathöfer Feb 03 '21 at 17:00