简体   繁体   English

如何比较datetimepicker输入的日期和当前日期

[英]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. 我有datetimepicker工具,用于输入员工的出生日期,我想检查用户是否忘记输入有效的出生日期,并将datetimepicker保留在当前日期,因此他会收到一条警报消息。
I've tried to write this code in ValueChanged event but it didn't worked as I wished ... any help 我试图在ValueChanged事件中编写此代码,但没有按我希望的那样工作……任何帮助

if (empRegBdatePicker.Value == DateTime.Today)
            {
                MessageBox.Show("Please enter a valid birth date of an employee");
                empRegBdatePicker.Focus();
            }

If you want to check just date, you can try this: 如果您只想查看日期,可以尝试以下操作:

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

Just simply write 只需写

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

Also, perform this validation before any save operations instead of some value changed events. 另外,请在执行任何保存操作之前执行此验证,而不要执行某些值更改事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM