2017-08-17 43 views
0

如何比較和驗證兩次? 我的場景是我需要添加各種時間表。假設person 1 has schedules on today 18/08/2017 as Sch1: 0800-1600 and sch2:2200-0600sch1 starts and ends on same daysch2 starts on 18/08/2017 and ends on 19/08/2017 6am. 現在,當我嘗試編輯Schedule結束時間時,我想添加2個驗證,即安排結束時間必須大於開始時間,並且還必須大於當前系統時間。如何驗證這兩個?當我添加驗證,然後在24小時後結束時間,2017/2017/2017的上午1點,上午2點,上午3點比18/08/2017的2000,2100(下午8點和9點)的值更低。如何比較和驗證2次MVC C#中的兩個日期?

我進行驗證編碼下面

if (D.ScheduleEndTime != T.EndTime && D.ScheduleEndTime < Time) 
{ 
    return Json(new { success = "fail", message = "End Time must be greater than current time!!!" }, JsonRequestBehavior.AllowGet); 
} 
else if (D.ScheduleEndTime != T.EndTime && D.ScheduleEndTime < T.StartTime) 
{ 
    return Json(new { success = "fail", message = "End Time must be greater than start time!!!" }, JsonRequestBehavior.AllowGet); 
} 

在上面的代碼d & T. d意味着進入,而編輯和T表示在數據庫表值的值

+1

您是否嘗試過使用System.DateTime類型? https://msdn.microsoft.com/zh-cn/library/system.datetime(v=vs.110).aspx –

+2

您的問題很不清楚。什麼是你真的有問題?你有什麼嘗試?什麼不工作?任何具體的錯誤?發生什麼行爲?這種行爲如何錯誤?在你寫問題時考慮這些類型的問題。你回答得越好,人們可以幫助你的機會就越大,具有諷刺意味的是,在這個過程中,你經常會自己找到答案。 –

+0

sry爲不明確的格式..我會更新我的問題 – Halim

回答

0

您沒有給出任何信息關於你正在使用的數據類型,但聽起來像你正在使用自定義日期時間對象而不是日期時間數據類型?

所以每個自定義日期時間對象都有類似如下結構的東西:

custom_datetime: 
- string Date (ie: 18/08/2017) 
- int startTime (ie: 2200) 
- int endTime (ie: 0600) 

在這種情況下,它會是這樣的,也許......

// Temp variable to manipulate, instantiated to passed in custom_datetime D. 
custom_datetime temp = D; 
// First check if the passed in date time is different than date time stored 
if ((temp.Date != T.Date) || ((temp.Date == T.Date) && (temp.endTime != T.endTime))) { 
    // Then check if the endTime is less than the startTime 
    // If it is, then the end time is the next day so increment the date in temp variable. Now use temp variable to do rest of the checks. 
    if (temp.endTime < temp.startTime) { 
     temp.Date++; // Increment to next day, so 18/08/2017 becomes 19/08/2017 
    } 

    //Check if passed in date < stored date, if it is, then it will also be less than startTime so return error. 
    if (temp.Date < T.Date) { 
     return Json(new { success = "fail", message = "End Time must be greater than current time!!!" }, JsonRequestBehavior.AllowGet); 
    } 
    //Check if the passed in date is the same as the stored date. If it is, make sure the endTime is greater than the stored startTime 
    else if ((temp.Date == T.Date) && (temp.endTime <= T.startTime)) { 
     return Json(new { success = "fail", message = "End Time must be greater than current time!!!" }, JsonRequestBehavior.AllowGet); 
    } 
    //Check if the passed in date < current system date or if the passed in date == current system date but the endTime <= the current time. 
    else if ((temp.Date < Time.Date) || ((temp.Date == Time.Date) && (temp.endTime <= Time.Time)) { 
     return Json(new { success = "fail", message = "End Time must be greater than current time!!!" }, JsonRequestBehavior.AllowGet); 
    } 
} 

的代碼實際上並不工作,但希望它能讓你知道你可以做的事情。需要更多信息才能給出確切的答案。