1

Possible Duplicate:
How check intersection of DateTime periods

Hello guys I have two datetime ranges old Check in, Check Out and New Check in, Check Out how i can compare this two date time ranges each other if Old Range contains or equals new Range?

Community
  • 1
  • 1
Irakli Lekishvili
  • 33,492
  • 33
  • 111
  • 169
  • 1
    Have you seen: http://stackoverflow.com/questions/7325124/how-check-intersection-of-datetime-periods – Ta01 Dec 02 '11 at 18:12

2 Answers2

1

Not exactly sure what you're trying to accomplish.

Simply put, you have 4 cases:

  1. Old range contains new range
  2. New range contains old range
  3. Old range starts before new range, but also ends before new range
  4. New range starts before old range, but also ends before old range

If you want to test all of these, you need to if, else if them all. But if you only care about case 1, you can test that by doing this sort of thing:

        var oldCheckout = DateTime.Now.AddMinutes(-500);
        var oldCheckin = DateTime.Now.AddMinutes(-30);
        var newCheckout = DateTime.Now.AddMinutes(-400);
        var newCheckin = DateTime.Now.AddMinutes(-50);

        if (oldCheckout < newCheckout && newCheckin < oldCheckin)
            return true;
        else
            return false;
Gaute Løken
  • 7,522
  • 3
  • 20
  • 38
0

you can simply compare like this:

 DateTime d1 = DateTime.Now.AddDays(3);
            DateTime d2 = DateTime.Now;

            if (d2 > d1)
            {
                Console.WriteLine("d1 is less than d2");

            }
            else
            {
                Console.WriteLine("d2 is less than d1");
            }
Peeyush
  • 4,728
  • 16
  • 64
  • 92