sys_debug
sys_debug

Reputation: 4003

ASP.NET C# Date comparison

I've read through some suggestions in the forum and this is closest thing I could relate to but not quite there yet Compare journey date with return date in asp.net c#

Anyway, the question is I am trying to book a room, from date x until date y. No other booking should take place at this time i.e. system sends off a message saying it can't complete overwriting booking. Any tutorial you could direct me to? or even sample code to build upon?

Thanks,

Upvotes: 0

Views: 3107

Answers (5)

sys_debug
sys_debug

Reputation: 4003

I came across something helpful myself, this is is MSDN: DateTime Subtract

and the code looks like this:

                    System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
        System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);
        System.DateTime date3 = new System.DateTime(1996, 10, 12, 8, 42, 0);

        // diff1 gets 185 days, 14 hours, and 47 minutes.
        System.TimeSpan diff1 = date2.Subtract(date1);

        // date4 gets 4/9/1996 5:55:00 PM.
        System.DateTime date4 = date3.Subtract(diff1);

        // diff2 gets 55 days 4 hours and 20 minutes.
        System.TimeSpan diff2 = date2 - date3;

        // date5 gets 4/9/1996 5:55:00 PM.
        System.DateTime date5 = date1 - diff2;

Nonetheless, I will try this, but thanks again for the help

Edit:

I've also come across this code from a site http://www.dotnetspider.com/forum/84579-How-To-Check-Date-Date-Range.aspx

DateTime sd=Convert.ToDateTime("2/2/2007");
DateTime ed =Convert.ToDateTime("2/2/2009");
if ((sd<=Convert.ToDateTime(nTextBox1.Text)) && (Convert.ToDateTime(nTextBox1.Text<=ed))
{
MessageBox.Show("In Range");
}
else
{
MessageBox.Show("Not In Range");
}

good for range check :D

Upvotes: 1

Joey
Joey

Reputation: 1800

How about:

private bool ConflictsWithExisting(Booking booking)
{
  return existingBookings.Any(b => b.ConflictsWith(booking));
}

with the following method on Booking:

public bool ConflictsWith(Booking booking)
{
  // You may want to check they are for the same room here.
  return !(booking.EndDate <= this.StartDate || booking.StartDate >= this.EndDate);
}

Upvotes: 1

amelvin
amelvin

Reputation: 9051

If it were me I would have a RoomBooking transactional table in the database (containing at least a RoomId column plus RoomTypeId,BookingStartDate and BookingEndDate). Then write a store procedure that queried the database to fulfill the use case "Is there a room available of an appropriate room type for a particular date range" - with a signature like:

GetAvailableRoomsForTypeAndDates(RoomTypeId, BookingStartDate, BookingEndDate)

The return from that is then handled in your code or via an ORM tool.

Upvotes: 0

Bas
Bas

Reputation: 27095

Assuming a Booking class with the properties RoomNumber, StartDate and EndDate.

class Booking
{
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int RoomNumber { get; set; }
}

bool IsRoomAvailableOnDate(int roomNumber, DateTime date)
{
    //change this to match your data source
    List<Booking> bookings = Booking.GetBookings();

    // get all bookings that have a start date and end date within your timeframe
    var bookingsWithinDate = from booking in bookings
                                where booking.RoomNumber == roomNumber
                                && booking.StartDate <= date
                                && booking.EndDate >= date
                                select booking;

    if (bookingsWithinDate.Any())
    {
        //bookings found that match date and room number
        return false;
    }
    else
    {
        //no bookings
        return true;
    }
}

Upvotes: 2

Guffa
Guffa

Reputation: 700342

When you are about to create a new booking, check against the existing bookings.

A booking doesn't overlap if ends before the existing booking or starts after it, so just loop throught the existing bookings and check:

if (!(newBooking.EndDate < existingBooking.StartDate || newBooking.StartDate > existingBooking.EndDate)) {
  // there is a conflicting booking
}

Upvotes: 3

Related Questions