Xamarin Community Toolkit DateTimeOffsetConverter
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 DatePicker
in Xamarin.Forms will only work with DateTime
. This converter can be used in those scenarios.
Syntax
<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>
Sample
DateTimeOffsetConverter sample page Source
You can see this in action in the Xamarin Community Toolkit Sample App.