使用DateTime实例比较2次

时间:2013-05-11 15:22:32

标签: c# asp.net-mvc datetime

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

我希望在约会和另一个约会之间离开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)
                    {

                    }
                }
            }

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

1 个答案:

答案 0 :(得分:2)

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