【问题标题】:ASP.NET C# Date comparisonASP.NET C# 日期比较
【发布时间】:2011-10-11 10:53:00
【问题描述】:

我已经阅读了论坛中的一些建议,这是我能想到的最接近的东西,但还不完全是Compare journey date with return date in asp.net c#

无论如何,问题是我想预订一个房间,从日期 x 到日期 y。此时不应进行其他预订,即系统会发送一条消息说它无法完成覆盖预订。有什么教程可以指导我吗?甚至是构建的示例代码?

谢谢,

【问题讨论】:

  • 真的看不出问题..我猜你有一个预订列表,其中包含一个 DateTime 开始和一个 DateTime 结束.. 使用 foreach 来检查你的列表并检查新的 dateTime对您的情况有效..

标签: c# asp.net date


【解决方案1】:

当您要创建新预订时,请检查现有预订。

如果在现有预订之前结束或之后开始,预订不会重叠,因此只需遍历现有预订并检查:

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

【讨论】:

    【解决方案2】:

    假设 Booking 类具有 RoomNumber、StartDate 和 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;
        }
    }
    

    【讨论】:

      【解决方案3】:

      怎么样:

      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);
      }
      

      【讨论】:

        【解决方案4】:

        我自己发现了一些有用的东西,这是 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

        【讨论】:

          【解决方案5】:

          如果是我,我会在数据库中有一个 RoomBooking 事务表(至少包含一个 RoomId 列加上 RoomTypeId、BookingStartDate 和 BookingEndDate)。然后编写一个查询数据库的存储过程来满足用例“在特定日期范围内是否有合适房间类型的可用房间” - 带有如下签名:

          GetAvailableRoomsForTypeAndDates(RoomTypeId, BookingStartDate, BookingEndDate)

          然后在您的代码中或通过 ORM 工具处理来自它的返回。

          【讨论】:

          • 上面的大多数 cmets 都展示了我已经知道的内容 :( 我需要弄清楚如何计算范围并阻止它在日期 x 和日期 y 之间被选择
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多