我已經通過在論壇上提出了一些建議閱讀,這是我可能涉及到最近的事情,但還沒有應用Compare journey date with return date in asp.net c#ASP.NET C#日期比較
無論如何,問題是,我想預訂房間,從x日期到y日期。目前還沒有其他預訂應該發生,即系統發送消息說它不能完成覆蓋預訂。你可以指導我的任何教程?或者甚至是代碼示例以構建?
謝謝,
我已經通過在論壇上提出了一些建議閱讀,這是我可能涉及到最近的事情,但還沒有應用Compare journey date with return date in asp.net c#ASP.NET C#日期比較
無論如何,問題是,我想預訂房間,從x日期到y日期。目前還沒有其他預訂應該發生,即系統發送消息說它不能完成覆蓋預訂。你可以指導我的任何教程?或者甚至是代碼示例以構建?
謝謝,
當您即將創建新預訂時,請檢查現有預訂。
甲預訂不如果兩端的現有預訂之前或之後重疊它啓動,所以只是環throught現有預訂,並檢查:
if (!(newBooking.EndDate < existingBooking.StartDate || newBooking.StartDate > existingBooking.EndDate)) {
// there is a conflicting booking
}
假設與屬性RoomNumber,起始日期和結束日期一個預定類。
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;
}
}
如果是我的話我會在數據庫中(至少含有RoomId列加RoomTypeId,BookingStartDate和BookingEndDate)一RoomBooking事務表。然後編寫查詢的數據庫,以滿足使用情況的存儲過程「是否有一個特定的日期範圍內提供適當的房間類型的房間」 - 與像簽名:
GetAvailableRoomsForTypeAndDates(RoomTypeId,BookingStartDate,BookingEndDate)
然後在您的代碼或通過ORM工具處理返回。
上面的大多數評論顯示了我已經知道:(我需要弄清楚如何計算範圍,並阻止它在日期x和日期y之間選擇 –
如何:
private bool ConflictsWithExisting(Booking booking)
{
return existingBookings.Any(b => b.ConflictsWith(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);
}
我碰到一些有助於來到自己,這是是 MSDN: DateTime Subtract
和代碼如下所示:
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;
不過,我會嘗試這一點,但再次感謝您的幫助
編輯:
我也碰到過這樣的代碼在網站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");
}
適合範圍檢查: D
不能真的看到問題.. 我想你有一個預訂列表,其中有一個DateTime開始和一個DateTime結束.. 使用foreach通過你的列表,並檢查是否新的日期時間是有效的爲您的條件.. – Rob