Xamarin Form: How do i change from current device time to different country time
Asked Answered
A

2

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?

Asexual answered 19/10, 2017 at 4:13 Comment(0)
B
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

Berri answered 11/12, 2019 at 8:5 Comment(0)
A
-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

Aluminum answered 19/10, 2017 at 5:48 Comment(4)
I have tested but it give me this error Exception of type 'System.TimeZoneNotFoundException' was thrown.Asexual
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 specificAsexual
Other problem : What if the TimeZone not support? Are there any other alternatives that can guarantee to get the time.Asexual
Always returns TimeZoneNotFoundException.Berri

© 2022 - 2024 — McMap. All rights reserved.