Search code examples
c#winformsdatetimepicker

How to compare the date entered by datetimepicker and current date


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

Solution

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

    if(empRegBdatePicker.Value.Date == DateTime.Now.Date)
    {
        //birthdate is today
    }