1

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?

Nahuel Ianni
  • 3,177
  • 4
  • 23
  • 30
Shiva Debrown
  • 167
  • 4
  • 5
  • 20
  • 1
    Look at the linked question, basically... – Jon Skeet Sep 04 '14 at 10:29
  • And if that doesn't help - can't you just fetch the `Value` from each `DateTimePicker` and compare them? What have you tried, and what went wrong? – Jon Skeet Sep 04 '14 at 10:29
  • I wanted to know how to use greater than and less than comparisons in a winform If I save my datetime it saves to my database as text(string) as 04/09/2014,If I want to compare these 2 strings in the form ,what to do? @JonSkeet – Shiva Debrown Sep 04 '14 at 10:36
  • What @JonSkeet was suggesting could be shown in my answer. – jbutler483 Sep 04 '14 at 10:40
  • From your comment, I would be asking myself as to how you would check if dates are before/after/equal if you have just converted them to strings FROM Date/Time format? – jbutler483 Sep 04 '14 at 10:42
  • @ShivaDebrown: Well you just use `foo.Value > bar.Value`... how your data is stored in your database is an entirely different matter. (Your question doesn't even mention a database!) You should change your database schema so that it's *not* using dates... – Jon Skeet Sep 04 '14 at 10:59
  • Of course , storing data is different matter..BUt trying to explain what I want exactly...and I'm not comparing database values so,didn't mention database @JonSkeet – Shiva Debrown Sep 04 '14 at 11:05
  • for example my company hires employees so that they are less than 30 years old.. if i Select date time picker my message box has to show that one's "age is this much" so you are not supposed to fit for this position,,, for this type of problems What to do.. @jbutler483 – Shiva Debrown Sep 04 '14 at 11:29
  • @ShivaDebrown - Why must you convert the dates to strings before you compare them? Why can't you just compare the .Value properties of each DateTimePicker? – Chris Dunaway Sep 04 '14 at 15:05
  • `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 "; }` will it work for to find age?? @jbutler483 – Shiva Debrown Sep 06 '14 at 04:22

2 Answers2

0

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

Karthick NS
  • 88
  • 1
  • 7
  • for example my company hires employees so that they are less than 30 years old.. if i Select date time picker my message box has to show that one's "age is this much" so you are not supposed to fit for this position,,, for this type of problems What to do.. @karthick – Shiva Debrown Sep 04 '14 at 11:30
  • Use ValueChanged event of the date picker control. From that event you verify the value. – Karthick NS Sep 04 '14 at 11:40
0

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 "; 
  }
Shiva Debrown
  • 167
  • 4
  • 5
  • 20