Question How to convert MaskedTextbox to DateTime?

raysefo

Well-known member
Joined
Jul 21, 2010
Messages
207
Programming Experience
Beginner
Hi,

I have a MaskedTextBox which is like below;

30/01/2011 08:42

I would like to convert this to DateTime, Is there a way to do it?

Thanks in advance.

Best Regards
 
Select the input mask 'short date and time' and validating type will be DateTime by default.
Call ValidateText method to get the parsed instance of the validating type, or use the e.ReturnValue from TypeValidationCompleted event.
You can learn more about the MaskedTextBox class from here: MaskedTextBox Class (System.Windows.Forms)
 
Johns' answer is absolutely correct but as an alternative that I think it's simpler:
you can also replace this masked text box for a DateTimePicker, and set 'ShowUpDown = True'; 'Format = Custom'; 'CustomFormat = dd/MM/yyyy hh:mm' that you automatically have a datetime type and don't have to validate and will give an effect very similar to the masked box.
 
Back
Top