how to compare two datetimepicker values in c# [duplicate]

ぐ巨炮叔叔 提交于 2019-12-08 03:41:40

问题


I have a problem with a Windows Form application which has two DateTimePicker controls showing "DATE OF BIRTH" and "DATE OF JOINING".

I want to compare the values of these controls such that date of birth should be not less than and not greater than and should not be equal less than and not greater than and should not be equal...

How can I do this?


回答1:


Try this code

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

Hope this will help




回答2:


As suggested by jbutler483

I tried to do but I couldn't get any solution finally as my own knowledge I used following code

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


来源:https://stackoverflow.com/questions/25663033/how-to-compare-two-datetimepicker-values-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!