簡體   English   中英

使用DateTime實例比較2次

[英]Comparing 2 Times using DateTime instances

我正在嘗試預約系統。 唯一的問題是,如果接受另一個約會,我不想讓客戶創建約會。

我想在約會和另一個約會之間離開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)
                    {

                    }
                }
            }

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM