-4

I'am struggling on how to make this on code. please help. I use c#

Here's the data:

    DateTime date_from = Convert.ToDateTime("2018/08/22 12:20:00");
    DateTime date_to =  Convert.ToDateTime("2018/08/28 07:25:00");

    DateTime other_date_from =  Convert.ToDateTime("2018/08/24 00:20:00");
    DateTime other_date_to=  Convert.ToDateTime("2018/08/25 00:21:00");

Now, I'd like to check if the data from date_from and date_to is within the range of other_date_from and other_date_to.

For example.

This datetime

"2018/08/22 12:20:00 to 2018/08/28 07:25:00" : August 22 12:20 PM to August 28 07:25 AM

is within the range of

"2018/08/24 00:20:00 to 2018/08/25 00:21:00" August 24 12:20 AM to August 25 12:21 AM

If its within the range, it returns true, if not return false.

MT-FreeHK
  • 2,462
  • 1
  • 13
  • 29
  • What does your code look like so far? And does the date range you provide have to fully overlap the range to be considered "in range"? – ProgrammingLlama Aug 26 '18 at 02:20
  • Possible duplicate of [How to check if a DateTime range is within another 3 month DateTime range](https://stackoverflow.com/questions/2688791/how-to-check-if-a-datetime-range-is-within-another-3-month-datetime-range) – dWinder Aug 26 '18 at 05:47

2 Answers2

0

This simple condition will do the trick !

date_from <= other_date_to && date_to >= other_date_from
Phenixer
  • 46
  • 5
0

It's bit of head scratcher sometimes...

            DateTime date_from = Convert.ToDateTime("2018/08/22 12:20:00");
            DateTime date_to = Convert.ToDateTime("2018/08/28 07:25:00");

            DateTime other_date_from = Convert.ToDateTime("2018/08/24 00:20:00");
            DateTime other_date_to = Convert.ToDateTime("2018/08/25 00:21:00");

            bool bInRange = ((other_date_from > date_from & other_date_from < date_to) &
                            (other_date_to > date_from & other_date_to < date_to));
Chris Catignani
  • 5,040
  • 16
  • 42
  • 49