1

I have datetimepicker tool that is used for employee birth date entry and I want to check if the user forget to enter the valid birth date and left the datetimepicker on the current date ,so he'll get an alert message.
I've tried to write this code in ValueChanged event but it didn't worked as I wished ... any help

if (empRegBdatePicker.Value == DateTime.Today)
            {
                MessageBox.Show("Please enter a valid birth date of an employee");
                empRegBdatePicker.Focus();
            }
Mouad Raizada
  • 127
  • 2
  • 15
  • Possible duplicate of [How to compare DateTime in C#?](http://stackoverflow.com/questions/3059497/how-to-compare-datetime-in-c) – M. Adeel Khalid Jan 26 '17 at 12:12
  • I think we need more information. Does the debugger hit the code above? What is the value of `empRegBdatePicker.Value`? In what way did it not work? – Baldrick Jan 26 '17 at 12:12
  • Do not write it in the ValueChanged event, if the user forgets to input their date of birth the value won't change so the event won't fire. – bolt19 Jan 26 '17 at 12:18

2 Answers2

4

If you want to check just date, you can try this:

if(empRegBdatePicker.Value.Date == DateTime.Now.Date)
{
    //birthdate is today
} 
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Nedim Hozić
  • 1,871
  • 15
  • 15
0

Just simply write

if(empRegBdatePicker < Today)
{
    //birthdate is in past
}

Also, perform this validation before any save operations instead of some value changed events.

Ali Baig
  • 3,819
  • 4
  • 34
  • 47