You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Converting to DateOnly/TimeOnly from string #59253
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).
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?
Activity
[-]Convering `DateOnly/TimeOnly` from string[/-][+]Converting to `DateOnly/TimeOnly` from string[/+]0xced commentedon Jan 27, 2022
It's a bummer that the
DateOnlyandTimeOnlytype converters have been forgotten in .NET 6. But this is the perfect opportunity to use the newIParseable<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 version6.0.2-mauipre.1.22054.8. For some reason version6.0.0doesn't work whe using the .NET 6.0.101 SDK and produces the following error:Here's how to implement the generic converter:
Then register
DateOnlyandTimeOnlyconverters globally:Of course, it would still be nice to have those converters built-in for .NET 7.
martincostello commentedon Jan 27, 2022
#62840
tarekgh commentedon May 1, 2022
Closing this as we are tracking the official proposal in #68743 and #68744