Xamarin表格:我如何从当前设备时间更改为不同的国家/地区时间

时间:2017-10-19 04:13:46

标签: datetime xamarin timezone currentculture

我对日期时间不太熟悉。我目前想知道如何将设备的现有时间转换为不同国家/地区的日期/时间。

E.g。 App.CurrentDate< - 显示设备设置日期/时间。我希望它在不同国家的时间选择不同的网站,网站可以是任何国家

有可能实现这个目标吗?

2 个答案:

答案 0 :(得分:0)

Android和iOS使用IANA时区名称。它们看起来像“美国/纽约”,您可以在tz database time zones列表中找到它们的列表。

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

参考https://xamarinhelp.com/time-zones-xamarin-forms/

答案 1 :(得分:-1)

首先以UTC格式获取当前日期和时间

var utcTime = DateTime.UtcNow;

然后将其转换为您需要的时区

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

还可以根据坐标识别时区 - this answer shows how

相关问题