Class DateTimeField
A form input control that allows users to select both a date and a time of day.
Inheritance
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
[UxmlElement]
public class DateTimeField : ExVisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IContextOverrideElement, IAdditionalDataHolder, IInputElement<DateTime>, IValidatableElement<DateTime>, INotifyValueChanging<DateTime>, INotifyValueChanged<DateTime>, ISizeableElement
Remarks
The DateTimeField is a composition rather than a new parser: it pairs the date entry rules of DateField with the segmented time entry of TimeField, and its picker popover stacks a DatePicker above a TimePicker.
Both halves read and write one DateTime. Picking a date preserves the time of day and changing the time preserves the calendar date, so neither half can silently reset the other.
Key features:
- A date segment with a configurable dateFormat, stepped a day at a time with the arrow keys
- A time segment with per-segment stepping, a 12 or 24-hour clock and optional seconds
- A combined calendar and spinner picker
- Optional min and max bounds, which clamp the value
- Size variants and value validation
NOTE: Like DateField, the date is formatted and parsed with the invariant culture, so the displayed order of the parts follows dateFormat and not the machine locale.
Examples
Basic usage in UXML:
<UXML xmlns:appui="Unity.AppUI.UI">
<appui:DateTimeField name="starts-at" />
</UXML>
A US-style 12-hour field bounded to a single day:
<UXML xmlns:appui="Unity.AppUI.UI">
<appui:DateTimeField
name="appointment"
size="Lg"
date-format="MM/dd/yyyy"
hour12="true"
minute-step="15"
value="2024-01-01 09:00:00"
min="2024-01-01 08:00:00"
max="2024-01-01 18:00:00" />
</UXML>
Creating and configuring a DateTimeField in C#:
var field = new DateTimeField
{
value = new DateTime(2024, 1, 1, 9, 0, 0),
minuteStep = 15,
showSeconds = false
};
field.validateValue = dt => dt.DayOfWeek != DayOfWeek.Sunday;
field.RegisterValueChangedCallback(evt => {
Debug.Log($"Selected: ");
});
Constructors
DateTimeField()
Default constructor.
Declaration
public DateTimeField()
Fields
dateInputUssClassName
The DateTimeField date input styling class.
Declaration
public static readonly string dateInputUssClassName
Field Value
| Type | Description |
|---|---|
| string |
defaultDateFormat
The default date format string.
Declaration
public const string defaultDateFormat = "yyyy-MM-dd"
Field Value
| Type | Description |
|---|---|
| string |
pickerButtonUssClassName
The DateTimeField picker button styling class.
Declaration
public static readonly string pickerButtonUssClassName
Field Value
| Type | Description |
|---|---|
| string |
pickerPaneUssClassName
The DateTimeField picker pane styling class, applied to the popover content.
Declaration
public static readonly string pickerPaneUssClassName
Field Value
| Type | Description |
|---|---|
| string |
separatorUssClassName
The DateTimeField separator styling class.
Declaration
public static readonly string separatorUssClassName
Field Value
| Type | Description |
|---|---|
| string |
sizeUssClassName
The DateTimeField size styling class.
Declaration
public static readonly string sizeUssClassName
Field Value
| Type | Description |
|---|---|
| string |
timeInputUssClassName
The DateTimeField time input styling class.
Declaration
public static readonly string timeInputUssClassName
Field Value
| Type | Description |
|---|---|
| string |
ussClassName
The DateTimeField main styling class.
Declaration
public static readonly string ussClassName
Field Value
| Type | Description |
|---|---|
| string |
Properties
contentContainer
The content container of this DateTimeField. This is null for DateTimeField.
Declaration
public override VisualElement contentContainer { get; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
Overrides
date
The date part of the value.
Declaration
public Date date { get; }
Property Value
| Type | Description |
|---|---|
| Date |
dateFormat
The format string of the date part. Defaults to yyyy-MM-dd.
Declaration
[CreateProperty]
[UxmlAttribute("date-format")]
public string dateFormat { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
hour12
Whether the time part uses a 12-hour clock with an AM/PM segment. Defaults to false.
Declaration
[CreateProperty]
[UxmlAttribute("hour12")]
public bool hour12 { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
invalid
The DateTimeField invalid state.
Declaration
[CreateProperty]
[UxmlAttribute]
public bool invalid { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
max
The latest accepted value. Defaults to MaxValue, meaning unbounded.
Declaration
[CreateProperty]
[UxmlAttribute]
public DateTime max { get; set; }
Property Value
| Type | Description |
|---|---|
| DateTime |
Remarks
Setting a bound clamps the current value into range immediately.
min
The earliest accepted value. Defaults to MinValue, meaning unbounded.
Declaration
[CreateProperty]
[UxmlAttribute]
public DateTime min { get; set; }
Property Value
| Type | Description |
|---|---|
| DateTime |
Remarks
Setting a bound clamps the current value into range immediately.
minuteStep
The number of minutes stepped per arrow press or spinner click. Defaults to 1.
Declaration
[CreateProperty]
[UxmlAttribute("minute-step")]
public int minuteStep { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
showSeconds
Whether the time part displays a seconds segment. Defaults to false.
Declaration
[CreateProperty]
[UxmlAttribute("show-seconds")]
public bool showSeconds { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
size
The DateTimeField size.
Declaration
[CreateProperty]
[UxmlAttribute]
public Size size { get; set; }
Property Value
| Type | Description |
|---|---|
| Size |
time
The time-of-day part of the value.
Declaration
public TimeSpan time { get; }
Property Value
| Type | Description |
|---|---|
| TimeSpan |
validateValue
The DateTimeField validation function.
Declaration
[CreateProperty]
public Func<DateTime, bool> validateValue { get; set; }
Property Value
| Type | Description |
|---|---|
| Func<DateTime, bool> |
value
The DateTimeField value.
Declaration
[CreateProperty]
[UxmlAttribute]
public DateTime value { get; set; }
Property Value
| Type | Description |
|---|---|
| DateTime |
Remarks
Methods
SetValueWithoutNotify(DateTime)
Sets the DateTimeField value without notifying the DateTimeField.
Declaration
public void SetValueWithoutNotify(DateTime newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| DateTime | newValue | The new DateTimeField value. It is clamped into the bounds. |
StepSegment(TimeSegment, int)
Steps a single segment of the time part, wrapping inside that segment's own unit.
Declaration
public void StepSegment(TimeSegment segment, int delta)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSegment | segment | The segment to step. |
| int | delta | The number of steps, negative to step down. |