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

。_饼干妹妹 提交于 2020-01-16 09:05:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!