2014-09-04 13 views
0

我有一個Windows Form應用程序,它有兩個DateTimePicker控件顯示和「DATE加盟」「出生日期」的問題。如何比較兩個的DateTimePicker值在C#

我想比較這些控件的值,使得出生日期應不小於且不大於,不應該等於小於且不大於不應該是平等的......

我該怎麼做?

+1

看看鏈接的問題,基本上... – 2014-09-04 10:29:11

+0

如果這不幫助 - 你不能只從每個DateTimePicker中獲取'Value'並比較它們嗎?你有什麼嘗試,出了什麼問題? – 2014-09-04 10:29:46

+0

我想知道如何使用比比較大於和小於在一個winform 如果我將日期時間保存到我的數據庫爲文本(字符串)保存爲04/09/2014,如果我想比較在這2串形式,該怎麼辦? @JonSkeet – 2014-09-04 10:36:59

回答

0

正如jbutler483

建議我試圖這樣做,但我不能最後得到任何解決方案,我自己的知識我用以下代碼

private void dtpdob_ValueChanged(object sender, EventArgs e) 
    { 
     int year = DateTime.Today.Year - dtpdob.Value.Year; 
     int month = DateTime.Today.Month - dtpdob.Value.Month; 
     int day = DateTime.Today.Day - dtpdob.Value.Day; 
     var a = year.ToString(); 
     var b = month.ToString(); 
     var c = day.ToString(); 
     if (day != 0 && month!=0 && year!=0) 
     { 
      if(day!=1) 
      txtage.Text = a + " Years " + b + " Months " + c + " Days "; 
      else 
       txtage.Text = a + " Years " + b + " Months " + c + " Day "; 
     } 
     else if (day == 0 && month == 0) 
      txtage.Text = a + " Years "; 
     else if (day == 0 && month != 0) 
      txtage.Text = a + " Years " + b + " Months "; 
     else 
      txtage.Text = a + " Years " + b + " Months " + c + " Days "; 
    } 
0

試試這個代碼

DateTimePicker dtBDay = new DateTimePicker(); 
dtBDay.Value = DateTime.Now.AddYears(-5); 
DateTimePicker dtJoin = new DateTimePicker(); 
dtJoin.Value = DateTime.Now; 
if (dtBDay.Value >= dtJoin.Value) 
{ 
    throw new Exception("Date of Join cannot be less than or equal to Date of Birth"); 
} 

希望這將有助於

+0

例如讓他們不到30歲..如果我選擇日期時間選擇器我的消息框,必須證明自己的「年齡這麼多」,所以你不應該適合這個職位我的公司僱用的員工,,,這種類型的問題該怎麼辦[email protected] – 2014-09-04 11:30:40

+0

使用日期選取器控件的ValueChanged事件。從那個事件中,你驗證了這個值。 – 2014-09-04 11:40:50