Skip to content

[Enhancement] DatePicker/TimePicker should support DateOnly/TimeOnly and be able to clear the value #1100

@KPixel

Description

@KPixel

Merging #1847

Summary

  • DatePicker and TimePicker should use the new DateOnly and TimeOnly structs instead of DateTime
  • The new fields should be nullable
  • The new fields should be added to the Core implementation of MAUI
  • The old properties should continue to exist in Controls, be marked as Obsolete, and wrap the new properties from Core

API Proposed:

Core:

public partial interface IDatePicker
{ 
  DateOnly? Value { get; set; }
  
  DateOnly? MinimumValue { get; }
  
  DateOnly? MaximumValue { get; }
}

public partial interface ITimePicker
{ 
  TimeOnly? Value { get; }
}

Controls:

public partial class DatePicker
{ 
  DateOnly? Value { get; set; }
  
  DateOnly? MinimumValue { get; set; }
  
  DateOnly? MaximumValue { get; set; }

  [Obsolete($"Use {nameof(Value)} instead.")]
  DateTime Date
  {
    get => Value?.ToDateTime(TimeOnly.MinValue) ?? DateTime.MinValue;
    set => Value = DateOnly.FromDateTime(value);
  }

  [Obsolete($"Use {nameof(MinimumValue)} instead.")]
  DateTime MinimumDate
  {
    get => MinimumValue?.ToDateTime(TimeOnly.MinValue) ?? DateTime.MinValue;
    set => MinimumValue = DateOnly.FromDateTime(value);
  }

  [Obsolete($"Use {nameof(MaximumValue)} instead.")]
  DateTime MaximumDate
  {
    get => MaximumValue?.ToDateTime(TimeOnly.MinValue) ?? DateTime.MinValue;
    set => MaximumValue = DateOnly.FromDateTime(value);
  }
}

public partial class TimePicker
{ 
  TimeOnly? Value { get; set; }

  [Obsolete($"Use {nameof(Value)} instead.")]
  TimeSpan Time
  {
    get => Value?.ToTimeSpan() ?? TimeSpan.Zero;
    set => Value = TimeOnly.FromTimeSpan(value);
  }
}

ORIGINAL POST:

Summary

I wish DatePicker had a Date property that was nullable, so that we could use it for optional dates.

API Changes

My suggestion is to follow how UWP/WinUI resolved this issue in a backward-compatible way: They added another property.

public System.DateOnly? SelectedDate { get; set; }

It would go in both DatePicker and IDatePicker (which doesn't need to be back-compat).

By the way, I used the new DateOnly instead of DateTime. But I'm fine if that's not feasible.

Intended Use Case

Currently, it is a pain to build an app that allows users to provide an optional date. One workaround is to add a checkbox to "activate" entering that date, but that is really not user-friendly.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions