问题
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?
回答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);
Reference: https://xamarinhelp.com/time-zones-xamarin-forms/
回答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
来源:https://stackoverflow.com/questions/46822738/xamarin-form-how-do-i-change-from-current-device-time-to-different-country-time