Skip to content

Converting to DateOnly/TimeOnly from string #59253

Closed

Description

@maxkoshevoi

Related to: dotnet/aspnetcore#36511

Description

DateTime can be converted from string (TypeDescriptor.GetConverter(typeof(DateTime)).ConvertFromString("01.01.0001")), while DateOnly and TimeOnly cannot. This causes some issues. For example, if they are added as query parameters in Asp.Net, they would be represented as 6 and 5 fields respectively instead of one string field (see dotnet/aspnetcore#36511 for more info).

For now, I've created a NuGet with custom type converters to solve this: https://github.com/maxkoshevoi/DateOnlyTimeOnly.AspNet/tree/main/DateOnlyTimeOnly.AspNet/Converters/Type

Are there any plans to enable string to DateOnly/TimeOnly conversion?

Regression?

No

Activity

  1. ghost added
    untriaged
    on Sep 17, 2021
  2. changed the title [-]Convering `DateOnly/TimeOnly` from string[/-] [+]Converting to `DateOnly/TimeOnly` from string[/+] on Sep 30, 2021
  3. added
    needs-further-triage
    api-suggestion
    and removed
    untriaged
    on Sep 30, 2021
  4. added this to the 7.0.0 milestone on Sep 30, 2021
  5. 0xced commented on Jan 27, 2022

    @0xced
    Contributor

    It's a bummer that the DateOnly and TimeOnly type converters have been forgotten in .NET 6. But this is the perfect opportunity to use the new IParseable<TSelf> preview feature and create a generic type converter in a few lines of code.

    Note that you'll have to add <EnablePreviewFeatures>true</EnablePreviewFeatures> in your csproj and add a reference to the System.Runtime.Experimental package. Make sure to use version 6.0.2-mauipre.1.22054.8. For some reason version 6.0.0 doesn't work whe using the .NET 6.0.101 SDK and produces the following error:

    [CS0246] The type or namespace name 'IParseable<>' could not be found (are you missing a using directive or an assembly reference?

    Here's how to implement the generic converter:

    using System;
    using System.ComponentModel;
    using System.Globalization;
    
    namespace SampleCode;
    
    public class ParseableTypeConverter<T> : TypeConverter where T : IParseable<T>
    {
        public static void Register()
        {
            TypeDescriptor.AddAttributes(typeof(T), new TypeConverterAttribute(typeof(ParseableTypeConverter<T>)));
        }
    
        public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
        {
            return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
        }
    
        public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
        {
            if (value is string stringValue)
            {
                return T.Parse(stringValue, culture);
            }
            return base.ConvertFrom(context, culture, value);
        }
    }

    Then register DateOnly and TimeOnly converters globally:

    ParseableTypeConverter<DateOnly>.Register();
    ParseableTypeConverter<TimeOnly>.Register();

    Of course, it would still be nice to have those converters built-in for .NET 7.

  6. martincostello commented on Jan 27, 2022

    @martincostello
    Member

    For some reason version 6.0.0 doesn't work when using the .NET 6.0.101 SDK

    #62840

  7. tarekgh commented on May 1, 2022

    @tarekgh
    Member

    Closing this as we are tracking the official proposal in #68743 and #68744

  8. ghost locked as resolved and limited conversation to collaborators on Jun 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.DateTimeneeds-further-triage

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions