05/05/2014 by Nitesh

How To Limit Dates in DateTimePicker Control in Windows Forms

Dear Friends,

Another small tip that may help in day-to-day code development. When working with DateTimePicker controls in Windows forms, we come across a situation where we need to block the control to have a minimum and maximum dates. For example, the DOB field cannot have dates greater than today and DateOfLeavingCompany field cannot have dates less than today. This post will explain a simple tip to resolve situations like this. To limit dates in a DateTimePicker control, we can set the MinDate and MaxDate properties of the control as below.


dateTimePickerContorl1.MinDate = DateTime.Now; //Minimum Date is today
dateTimePickerContorl1.MaxDate = DateTime.Now.AddMonth(1); //Maximum Date is 1 month from today


dateTimePickerContorl1.MinDate = DateTime.Now 'Minimum Date is today
dateTimePickerContorl1.MaxDate = DateTime.Now.AddMonth(1) 'Maximum Date is 1 month from today

Hope this tip helps you! Keep learning and sharing!