0

I am not too familiar with date time. I am currently wonder how can I convert the existing time of the device to a different countries' date/time.

E.g. App.CurrentDate <- which display the device setting date/time. I want it to be in different country's time when choosing different site where the site can be any countries

Is it possible to achieve this?

LittleFunny
  • 8,155
  • 15
  • 87
  • 198

2 Answers2

1

Android and iOS use IANA timezone names. They look like this “America/New_York” and you can find a list of them at the List of tz database time zones.

TimeZoneInfo estZone = TimeZoneInfo.FindSystemTimeZoneById("America/New_York");
DateTime estTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, estZone);

References:

1 http://www.xamarinhelp.com/time-zones-xamarin-forms/

2 https://help.syncfusion.com/xamarin/scheduler/timezone

Divyesh
  • 2,085
  • 20
  • 35
-2

Grab the current date and time in UTC format first

var utcTime = DateTime.UtcNow;

Then convert it to whichever timezone you need

TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DateTime zoneTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, zone);

Here is how to get a list of all the time zones

More details about UTC

It's also possible to identify the timezone based on co-ordinates - this answer shows how

Steve Chadbourne
  • 6,873
  • 3
  • 54
  • 82
  • I have tested but it give me this error Exception of type 'System.TimeZoneNotFoundException' was thrown. – LittleFunny Oct 19 '17 at 08:45
  • When I put NZ instead of New Zealand Standard Time, it will work on both iOS and Android. The Central Standard Time will lead me to exception. I wonder will be device specific – LittleFunny Oct 19 '17 at 09:22
  • Other problem : What if the TimeZone not support? Are there any other alternatives that can guarantee to get the time. – LittleFunny Oct 19 '17 at 09:48
  • Always returns TimeZoneNotFoundException. – Divyesh Dec 11 '19 at 07:10