XamarinCommunityToolkit icon indicating copy to clipboard operation
XamarinCommunityToolkit copied to clipboard

[Bug] UtcDateTimeToLocalStringConverter does not support short date time format string

Open DuncanMT opened this issue 3 years ago • 1 comments

Description

UtcDateTimeToLocalStringConverter class throws ArgumentException if the format string is short date "d".

The implementation of the [IsValidDateFormat method] (https://github.com/xamarin/XamarinCommunityToolkit/blob/main/src/CommunityToolkit/Xamarin.CommunityToolkit/Converters/UtcDateTimeToLocalStringConverter.shared.cs#L55) returns false when using the "d" format string.

Stack Trace

System.ArgumentException: Value must be a valid date time format
Parameter name: DateTimeFormat
  at Xamarin.CommunityToolkit.Converters.UtcDateTimeToLocalStringConverter.Convert (System.Object value, System.Type targetType, System.Object parameter, System.Globalization.CultureInfo culture) <0x1188e1c80 + 0x00026> in <4d8fe5c5405e44d08fa28bbb90fc91b0>:0 

Immediate Window Test Breakdown of IsValidDateFormat Method Implementation

DateTime.UtcNow.ToString("d", CultureInfo.InvariantCulture)
"07/19/2022"

DateTime.TryParse("07/19/2022", out _)
false

Link to Reproduction Sample

https://github.com/DuncanMT/UtcDateTimeToLocalStringError

Steps to Reproduce

  1. Use UtcDateTimeToLocalStringConverter with a "d" format string.

Expected Behavior

DateTime correctly converted to local time and displayed as the short date format.

Actual Behavior

System.ArgumentException: Value must be a valid date time format
Parameter name: DateTimeFormat

Basic Information

  • Version with issue: 2.0.4
  • Last known good version:
  • IDE: JetBrains Rider 2022.1.2
  • Platform Target Frameworks: Found on iOS and Android, other platforms not tested.
    • iOS: 15.10.0.5 (Visual Studio Community)
    • Android: 12.3.3.3 (Visual Studio Community)
    • UWP: n/a
  • Nuget Packages: Xamarin.Forms Version="5.0.0.2478"

Workaround

Fully specifying format string "dd/MM/yyyy" works but of course this eliminates the localisation advantage of using "d".

DuncanMT avatar Jul 19 '22 14:07 DuncanMT

@DuncanMT I am the original author of this converter, I can check this out shortly.

@jfversluis Are we still working on this library or is it just accepting bug fixes?

LeoJHarris avatar Aug 02 '22 03:08 LeoJHarris