windowsforms-120742-controls-and-libraries-editors-and-simple-controls-examples-how-to-enter-date-time-values.md
The Editors library contains dedicated editors (DateEdit, TimeEdit and DateTimeOffsetEdit controls) to display and edit date-time values. However, you can allow any text editor to accept date-time values, as shown below.
Tip
To enter time intervals, use TimeSpanEdit.
The DateEdit, TimeEdit and DateTimeOffsetEdit controls provide native support for date-time value input. Users can enter values in the edit box according to a specific pattern (mask) and select values from the editor’s drop-down list.
Use the Mask Settings dialog to change the input pattern (mask) at design time. You can use one of the following actions to open this dialog:
The Mask Settings dialog allows you to specify a mask and customize mask options.
To specify an input mask in code, use the Properties.EditMask or Properties.MaskSettings.MaskExpression property.
dateEdit1.Properties.MaskSettings.MaskExpression = "g"; //"Short date/short time" pattern
DateEdit1.Properties.MaskSettings.MaskExpression = "g" ' "Short date/short time" pattern
For information on how to specify a mask at the data source layer, see Mask Type: Date-time. This topic also lists the available mask specifiers.
Check the Advanced Settings check box in the Mask Settings dialog to show mask options.
These options include:
The Mask Type: Date-time topic describes these options in more detail.
Use the following properties to get and set values for standalone editors:
These properties are synchronized with the editor’s EditValue property, which is of the object type.
Use the EditValue property for data binding.
To allow a text editor (TextEdit or its descendant) to accept date-time values, do the following:
Select the “DateTime”, “DateOnly”, “TimeOnly”, or “DateTime with Offset” mask type.
Specify an input mask and mask settings (optional).
Set the editor’s initial value to a DateTime / DateOnly / TimeOnly / DateTimeOffset value, or bind the editor to a data source field that contains values of these types.
The following example applies the “g” date-time mask to a text editor:
using DevExpress.XtraEditors.Mask;
var settings = textEdit1.Properties.MaskSettings.Configure<MaskSettings.DateTime>();
settings.MaskExpression = "g";
settings.UseAdvancingCaret = true;
textEdit1.Properties.MaskSettings.UseMaskAsDisplayFormat = true;
textEdit1.EditValue = DateTime.Now;
Imports DevExpress.XtraEditors.Mask
Dim settings = TextEdit1.Properties.MaskSettings.Configure(Of MaskSettings.DateTime)()
settings.MaskExpression = "g"
settings.UseAdvancingCaret = True
TextEdit1.Properties.MaskSettings.UseMaskAsDisplayFormat = True
TextEdit1.EditValue = DateTime.Now
The text editor automatically converts the entered text to a DateTime / DateOnly / TimeOnly / DateTimeOffset value and assigns this value to the editor’s EditValue property.
See Also