[Proposal] DateTimeOffsetConverter
See original GitHub issueDateTimeOffsetConverter
- Proposed
- Prototype
- Implementation: Not Started
- iOS Support
- Android Support
- macOS Support
- Windows Support
- Unit Tests
- Documentation: Not Started
- Sample
Summary
The DateTimeOffsetConverter
is a converter that allows users to convert a DateTimeOffset
to a DateTime
. Sometimes a DateTime
value is stored with the offset on a backend to allow for storing the timezone in which a DateTime
originated from. Controls like the Microsoft.Maui.Controls.DatePicker
only work with DateTime
. This converter can be used in those scenarios.
Detailed Design
DateTimeOffsetConverter.shared.cs
public class DateTimeOffsetConverter : ValueConverterExtension, IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, System.Globalization.CultureInfo culture)=> value is DateTimeOffset dateTimeOffset
? dateTimeOffset.DateTime
: throw new ArgumentException("Value is not a valid DateTimeOffset", nameof(value));
public object ConvertBack(object? value, Type targetType, object? parameter, System.Globalization.CultureInfo culture) => value is DateTime dateTime
? dateTime.Kind switch
{
DateTimeKind.Local => new DateTimeOffset(dateTime, DateTimeOffset.Now.Offset),
DateTimeKind.Utc => new DateTimeOffset(dateTime, DateTimeOffset.UtcNow.Offset),
_ => new DateTimeOffset(dateTime, TimeSpan.Zero),
}
: throw new ArgumentException("Value is not a valid DateTime", nameof(value));
}
Usage Syntax
XAML Usage
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
x:Class="MyLittleApp.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<xct:DateTimeOffsetConverter x:Key="DateTimeOffsetConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<Label Text="{Binding MyDateTimeOffset, Converter={StaticResource DateTimeOffsetConverter}}" />
</StackLayout>
</ContentPage>
C# Usage
class MyPage : ContentPage
{
public MyPage()
{
Content = new StackLayout
{
Children =
{
new Label().Bind(Label.TextProperty, nameof(ViewModel. MyDateTimeOffset), converter: new DateTimeOffsetConverter()),
};
}
}
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
datetimeoffsetconverter.md
The DateTimeOffsetConverter is a converter that allows users to convert a DateTimeOffset to a DateTime . Sometimes a DateTime value is stored ...
Read more >DateTimeOffsetConverter Class (System.ComponentModel)
Provides a type converter to convert DateTimeOffset structures to and from various other representations.
Read more >Passing DateTimeOffset as WebAPI query string
I was able to get this approach to work by copying the source of the framework DateTimeOffsetConverter , and then adding the line...
Read more >DateTimeOffsetConverter - .NET MAUI Community Toolkit
The DateTimeOffsetConverter is a converter that allows users to convert a DateTimeOffset to a DateTime.
Read more >DateTimeOffsetConverter.cs source code in C# .NET
This converter should behave just like DateTimeConverter only it should convert DateTimeOffsets. The code was copied from DateTimeConverter and adapted for ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@CliffAgius no you linked it fine. The proposal won’t automatically be closed as a result of the docs PR being closed. We need to manually drag the issue over to the complete column in the Project board. This is down to the fact that a feature/proposal should not be merged before the docs are done. Don’t worry I’ll sort this proposal.
Thanks again for the contribution!
O shoot, I should be more aware of if the actual feature is merged as well. My bad. 😬
I guess this one is done now though with the Docs also being complete?