Formatting Month and Date for Localization in Xamarin.Forms

JesseLibertyJesseLiberty USInsider, University ✭✭✭
edited January 2017 in Xamarin.Forms

I have the following code:

<Label Text="{Binding startTime, StringFormat='{0:MMM d, yyyy }'}" which renders June 18, 2017

I'd like to create a string that renders 18 June 2017 when the culture is not US.

Any ideas on how to do that?

Thanks.

Posts

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @JesseLiberty

    First consideration: Do you really want to get into micromanaging those displays instead of just taking the formatting set on the device by the user? If you take the device globalization format then you aren't responsible for formatting and your app honors the same values as the rest of the device making the user feel at home in your app.

    If you really do want to for this, I'd probably just make a DateFormatConverter that takes the DateTime object and returns a formatted string. That way you can put in one small consideration now: If USA then this, if !USA then that... And still be able to add conditions easily and in one place a year from now when the boss says: "French regulation says it has to be like this"

  • DavidDancyDavidDancy AUMember ✭✭✭✭

    Can you use a static resource as the format string? Perhaps set it on startup based on the detected locale?

  • tribaltribal USMember ✭✭✭
    edited January 2017

    This works for me <Label Text="{Binding StartTime, StringFormat='{0:d MMMM yyyy}"\> Also I have got another variant where

    <Label Text="{Binding StartTime, StringFormat='The start time was on {0:d MMMM yyyy \\a\\t HH:mm}"\>

    works as well

Sign In or Register to comment.