Forum Xamarin.Forms

DatePicker UTC

ShimmyWeitzhandlerShimmyWeitzhandler USMember ✭✭✭
edited August 2017 in Xamarin.Forms

Hi,

I'm using Entity Framework and ASP.NET Core Web API as a back end of my LoB app. In the server side, I want to store all DateTimes as UTC, while in the client side I want them to be displayed in local/preset timezone.

I'm using shared entities between the client and server.

I'd like to ask you what can I do in order to:

  • Convert all the DateTimes to UTC when storing it
  • Convert them to local/custom time-zone when displayed
  • Since my app has many DateTimes, I obviously prefer an automated and generalized approach, rather than messing with each property individually
  • This behavior is expected throughout the entire app, no exceptions needed to be taken care of

Any input, thoughts, links and ideas will be really appreciated.
Thank you very much ya'll!

Best Answer

  • JohnHardmanJohnHardman GB mod
    Accepted Answer

    @ShimmyWeitzhandler - Unless there is a particular requirement that prevents it, I would simply use ValueConverters to convert between UTC and local time, so that everything (including view models) other than what the user sees is done using UTC.

Answers

  • JorgeDiegoJorgeDiego USMember ✭✭

    Hi @ShimmyWeitzhandler

    If I were you I would covert every DateTime to UTC format before you send information to your web API (DateTime.ToUniversalTime()) and convert back, to local time, when you receive information from the web API (DateTime..ToLocalTime())

    As you have many DateTime objects, you can make a process that make these changes by reflection, it means, search every DateTime property and change them to UTC/Local time.

    If you make the conversion in the web API, you have to take in account that the server could be in a different time zone than the celullar.

    Hope it helps you.

  • JohnHardmanJohnHardman GBUniversity mod
    Accepted Answer

    @ShimmyWeitzhandler - Unless there is a particular requirement that prevents it, I would simply use ValueConverters to convert between UTC and local time, so that everything (including view models) other than what the user sees is done using UTC.

  • RaphaelSchindlerRaphaelSchindler USMember ✭✭✭
    edited August 2017

    @ShimmyWeitzhandler Do you display the dates in Labels? If you're using Bindings you can do this:

    myLabel.SetBinding(Label.TextProperty, new Binding(path: "myDate", stringFormat: "0: {MM.dd.yyyy}", source: MyClass);

    You can do this for entry etc also. No need to convert anything. If your app is global then make a ValueConverter

    Edit: Well, reading is hard... I think this works with DatePickers also.

  • JohnHardmanJohnHardman GBUniversity mod

    Best to avoid hard-coding date formats though (I'm in a part of the world where we use day/month/year, not month/day/ year) :-)

  • RaphaelSchindlerRaphaelSchindler USMember ✭✭✭

    @JohnHardman Depends. Our app is only for Germany, Austria and Switzerland so I can do this. That's why I wrote that he should use a ValueConverter if the app is for different countries.

    So I just tested it and you can do this:

    datePicker.SetBinding(DatePicker.FormatProperty, new Binding(path: "myDate", stringFormat: "dd.MM.yyyy or whatever you need", source: MyClass))

  • ShimmyWeitzhandlerShimmyWeitzhandler USMember ✭✭✭

    Thank you so much @JorgeDiego, @JohnHardman and @RaphaelSchindler

    The idea of using a converter seems to me the most logical, using all times as UTC and only display it as local/custom time.

Sign In or Register to comment.