-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Converting to DateOnly/TimeOnly
from string
#59253
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsRelated to: dotnet/aspnetcore#36511 Description
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 Regression?No
|
DateOnly/TimeOnly
from stringDateOnly/TimeOnly
from string
It's a bummer that the Note that you'll have to add
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 ParseableTypeConverter<DateOnly>.Register();
ParseableTypeConverter<TimeOnly>.Register(); Of course, it would still be nice to have those converters built-in for .NET 7. |
|
Related to: dotnet/aspnetcore#36511
Description
DateTime
can be converted from string (TypeDescriptor.GetConverter(typeof(DateTime)).ConvertFromString("01.01.0001")
), whileDateOnly
andTimeOnly
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
toDateOnly/TimeOnly
conversion?Regression?
No
The text was updated successfully, but these errors were encountered: