Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Android DateTime ToString appears to return Universal time while iOS returns Local

$
0
0

I've got to be doing something wrong here because DateTime has to be a well-used type. So the real question is: What am I doing wrong?

Steps to reproduce:

  1. Create a Xamarin.Forms Blank App (PCL)
  2. Replace App class with:

    using System;
    using System.Globalization;
    
    using Xamarin.Forms;
    
    namespace YourNamespace
    {
        public class App : Application
        {
            Label label = new Label {
                XAlign = TextAlignment.Center,
                Text = "Welcome to Xamarin Forms!"
            };
    
            public App ()
            {
                // The root page of your application
                MainPage = new ContentPage {
                    Content = new StackLayout {
                        VerticalOptions = LayoutOptions.Center,
                        Children = {
                            label
                        }
                    }
                };
    
                var stringTime = "02:45 AM";
                DateTime parsedTime;
                if (DateTime.TryParseExact(stringTime, "h:mm tt", null, DateTimeStyles.AssumeUniversal, out parsedTime))
                {
                    label.Text = String.Format ("{0} | {1} | {2}", stringTime, parsedTime, parsedTime.Kind);
                }
            }
        }
    }
    
  3. iOS displays "02:45 AM | 1/27/2015 9:45:00 PM | Local"

  4. Android displays "02:45 AM | 1/28/2015 2:45:00 PM | Local"

It appears that both have the correct underlying universal time, but why does Android appear to return universal time and iOS return local time (for US east coast not daylight saving)? My attempts to fix it by using DateTime.SpecifyKind (parsedTime, DateTimeKind.Utc) just made the situation worse.

Thanks!

Terry Westley

Xamarin Studio 5.7
Xamarin.Forms 1.3.1.6296
Xamarin.iOS 8.6.0.51 (Indie)
Xcode 6.1.1
Xamarin.Android 4.20.0.28 (Indie)
Xamarin Android Player 0.2.5
Mac Mini (late 2012)
Mac OS X 10.10.2


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>