Forum Cross Platform with Xamarin

DateTime formatting not followed

NetworkappNetworkapp NLMember ✭✭

For some time now I've once in a while encountered that sending this:

 var marker = DateTime.UtcNow;

 Dictionary<string, object> data = new Dictionary<string, object>
 {
    { "date_updated", marker.ToString("yyyy-MM-ddTHH:mm:ss") }
 };

actually results in:

 {
    date_updated: 2017-08-28T09.55.21, 
 }

So for some reason although the : are explicit in the formatting, they are replaced by dots in some cases.

It used to happen on some iOS devices once every few months. However, now I'm also noticing this happening almost daily on one or more Android devices. It leads to annoying errors because neither sqlite nor our server expects this different format.

Anyone has a clue why this happens sometimes and why it's not reproducible? I'm guessing it's some kind of locale thing overriding the formatting?

Posts

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    edited August 2017

    try with

    { "date_updated", marker.ToString("yyyy-MM-ddTHH':'mm':'ss") }
    

    csharp-examples.net/string-format-datetime/

  • NetworkappNetworkapp NLMember ✭✭

    Thanks, I just figured that out as well.

    As often asking the question answers it. So for anyone interested in why this is needed: Apparently in C# there is a TimeSeperator property on each culture and the standard placeholder for it is : and it will get replaced by whatever is set as TimeSeperator.

    See https://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.timeseparator(v=vs.110).aspx

    (Alternative solution should be to use InvariantCulture which has known properties)

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    The next question is whether or not you really want to override that native time separator.

    There is a reason its there: To make your software seem native and correct in all cultures. When you force ":" in your app where everything else on that person's phone shows "." in accordance with their locality and their OS preferences it makes your program stand out-and not in a good way.

    It would be like forcing the date order to American MonthDayYear for people in Europe where it is natively DayMonthYear - if you do that you piss off people and they get this sense of "Bloody Yanks think theirs is the only right way".

Sign In or Register to comment.