Skip to content

DateTimePicker returns mixed DateTimeKind values (Unspecified vs Local) #4551

@XingbinChen

Description

@XingbinChen

Hi,
I’m using the DateTimePicker control and binding its SelectedDateTime property to a DateTime? property in my ViewModel.

Bug

If I pick the date first, the SelectedDateTime.Kind is Unspecified.

If I pick the time first, the Kind becomes Local.

Why it happens

Looking at the source:

CalendarSelectedDateChanged (inside the DateTimePicker.calendar part) returns a DateTime with Kind == Unspecified.

ClockSelectedTimeChanged (inside TimePickerBase) sets SelectedDateTime = DateTime.Today when the previous value is null; DateTime.Today has Kind == Local.

Why this is a problem

Consumers of the control have to handle two different DateTimeKind cases, which is easy to overlook and may introduce bugs.

Possible fix

Replace DateTime.Today with DateTimeOffset.Now.Date (or another DateTime that keeps Kind == Unspecified) so that SelectedDateTime.Kind is consistently Unspecified, no matter which part (date or time) the user selects first.

Environment

MahApps.Metro version: v2.4.10
Target Framework: .Net 8

Screenshots

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions