tomasslavicek

Correct DateTime.Now on Xamarin.Android

Sep 27th, 2014
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.39 KB | None | 0 0
  1. // Does not work on Xamarin.Android (cs-cz locale)
  2. // - returns 8:00 instead of correct 9:00 (system time on the phone)
  3. DateTime now1 = DateTime.Now;
  4. DateTime now2 = DateTime.Now.ToLocalTime();
  5.  
  6. // Working solution:
  7. DateTime nowutc = DateTime.UtcNow;
  8. DateTime correctedNow = TimeZoneInfo.ConvertTime(nowutc, TimeZoneInfo.Utc,
  9.     TimeZoneInfo.FindSystemTimeZoneById(TimeZoneInfo.Local.Id));
Advertisement
Add Comment
Please, Sign In to add comment