【问题标题】:Comparing 2 Times using DateTime instances使用 DateTime 实例比较 2 次
【发布时间】:2013-05-06 02:33:30
【问题描述】:

我正在尝试预约系统。唯一的问题是,如果接受了另一个约会,我不想让客户创建约会。

我想在预约之间留出 1 小时,另一个说如果预约 A 是在 12:00,那么您不能在 12:00 到 13:00 之间进行预约

这是我的代码:

List<Appointment> acceptedAppointments = new Service1Client().getAllAcceptedAppointments();

获取所有已接受的约会。

foreach (Appointment item in acceptedAppointments)
            {
                if (item.Appointment_DateTime.Date == myDate.Date)
                {
                    if (myDate.AddHours(1) > item.Appointment_DateTime)
                    {

                    }
                }
            }

如果有人可以提供帮助,我不知道我需要在这里做什么,非常感谢!

【问题讨论】:

    标签: c# asp.net-mvc datetime


    【解决方案1】:
    bool isValidAppointment = true;
    
    // Go through all accepted appointments
    foreach (Appointment item in acceptedAppointments)
    {
        // Check if the difference between the appointments is less than 60 minutes
        if (item.Appointment_DateTime.Substract(myDate).Duration.TotalMinutes < 60)
        {
            // If so, set bool to indicate invalid appointment and stop validation
            isValidApopintment = false;
            break;
        }
    }
    
    if (isValidAppointment)
    {
        // Handle valid appointment
    }
    else
    {
        // Handle invalid appointment
    }
    

    这可以缩短为:

    bool isValidApointment = acceptedAppointments.Any(x => x.Substract(myDate).Duration.TotalMinutes < 60);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      相关资源
      最近更新 更多