question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Proposal] DateTimeOffsetConverter

See original GitHub issue

DateTimeOffsetConverter

  • 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:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
bijingtoncommented, Mar 22, 2022

@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!

1reaction
jfversluiscommented, Mar 22, 2022

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found