2017-08-17 11 views
0

2回比較して検証する方法は? 私のシナリオでは、さまざまなスケジュールを追加する必要があります。 person 1 has schedules on today 18/08/2017 as Sch1: 0800-1600 and sch2:2200-0600sch1 starts and ends on same dayであるのに対し、sch2 starts on 18/08/2017 and ends on 19/08/2017 6am. と仮定します。スケジュールの終了時刻を編集しようとすると、スケジュールの終了時刻が開始時刻よりも大きく、現在のシステム時刻よりも大きくなければならないという2つの検証を追加します。これら2つを検証する方法は? 24時間後に検証を追加すると、18/08/2017の2000,2100(午後8時9分)の値よりも、19/08/2017の1 am2am3amになります。MVC C#で日付を2回比較して2回検証する方法は?

上記符号D & T. Dにおいて

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

以下の検証のための私のコードは、編集中に入力された値を意味し、Tは、表の値は、データベース

+1

System.DateTimeタイプを試しましたか? https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx –

+2

あなたの質問は非常に不明です。あなたが実際に問題を抱えているのは何ですか?何を試しましたか?何がうまくいかないの?特定のエラー?どのような動作が起こっていますか?その行動はどうやって間違っていますかあなたの質問を書いている間、これらのタイプの質問を考えてみてください。あなたがそれらに答えるほうが良いほど、誰かがあなたを助ける機会が大きくなり、皮肉なことに、あなたはしばしば自分自身の答えにつまずくでしょう。 –

+0

不明な書式のためにsry ..私は私の質問を更新します – Halim

答えて

0

に何も情報を与えていない手段あなたが使用しているデータ型では、datetimeデータ型ではなくカスタムの日時オブジェクトを使用しているようですね。

カスタムの日付時刻オブジェクトのそれぞれが次の構造に似た何かを持っているので:その場合は

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

コードは実際には機能しませんが、うまくいけば、あなたができることのアイデアが得られます。正確な答えを得るためには、より多くの情報が必要です。

関連する問題