简体   繁体   English

wp8 C#中的时区转换

[英]timezone conversion in wp8 C#

I'm wondering there is no way to convert a date and time of a timezone to another!! 我想知道没有办法将时区的日期和时间转换为另一个! I've got numerous examples about timezone conversions and all are on non WP SDK. 我有很多有关时区转换的示例,并且都在非WP SDK上。 Amazingly TimeZoneInfo class of namespace System of WP SDK has no method for FindSystemTimeZoneById() . 令人惊讶的是,WP SDK的命名空间SystemTimeZoneInfo类没有FindSystemTimeZoneById()的方法。

Here Converting time between Timezones and How to convert a datetime to specific timezone in c#? 这里在时区之间转换时间以及如何在C#中将日期 时间 转换为特定时区? are very nice examples of converting timezones on .net except WP SDK. 除了WP SDK以外,都是在.net上转换时区的非常好的示例。 Suppose a scenario, I have a time in W. Australia Standard Time, now I need to convert that time to Eastern Standard Time. 假设有一个场景,我有一个在W.澳大利亚标准时间的时间,现在我需要将该时间转换为东部标准时间。 Normally I would do that like this : 通常我会这样:

string timeZoneStringOne = "W. Australia Standard Time";
TimeZoneInfo timeZoneIDOne = TimeZoneInfo.FindSystemTimeZoneById(timeZoneStringOne);

string timeZoneStringTwo = "Eastern Standard Time";
TimeZoneInfo timeZoneIDTwo = TimeZoneInfo.FindSystemTimeZoneById(timeZoneStringTwo);

DateTime dt = new DateTime(2010, 02, 08, 05, 00, 00);

DateTimeOffset dtOffset1 = new DateTimeOffset(dt, timeZoneIDOne.GetUtcOffset(dt));
Console.WriteLine(dtOffset1);

DateTimeOffset dtOffset2 = TimeZoneInfo.ConvertTime(dtOffset1, timeZoneIDTwo);
Console.WriteLine(dtOffset2);

DateTimeOffset dtOffset3 = TimeZoneInfo.ConvertTime(dtOffset2, timeZoneIDOne);
Console.WriteLine(dtOffset3);

Console.ReadKey();

/*
  Output :
  2/8/2010 5:00:00 AM +08:00
  2/7/2010 4:00:00 PM -05:00
  2/8/2010 5:00:00 AM +08:00
*/

But how can I do it on windows phone 8 ??? 但是我该如何在Windows Phone 8上呢?

Unfortunately, the TimeZoneInfo object is crippled on WP8 and WinRT, in that there is no FindSystemTimeZoneById , or even an Id property. 不幸的是, TimeZoneInfo对象在WP8和WinRT上是残缺的,因为没有FindSystemTimeZoneById ,甚至没有Id属性。

IMHO, the best solution is to use Noda Time instead. 恕我直言,最好的解决方案是改用Noda Time When compiled as a Portable Class Library (PCL), it can run on WP8 just fine. 当编译为可移植类库(PCL)时,它可以在WP8上正常运行。 PCL is the default build in the latest NuGet package, so you should simply be able to use it. PCL是最新的NuGet软件包中的默认版本,因此您应该可以使用它。

However, you will need to use IANA time zones instead of Microsoft's, as the BCL time zone provider is not available in the portable version. 但是,由于BCL时区提供程序在便携式版本中不可用,因此您将需要使用IANA时区而不是Microsoft的时区。

// Get the input value
LocalDateTime ldt1 = new LocalDateTime(2010, 2, 8, 5, 0, 0);
DateTimeZone tz1 = DateTimeZoneProviders.Tzdb["Australia/Perth"];
ZonedDateTime zdt1 = ldt1.InZoneLeniently(tz1);

// Convert to the target time zone
DateTimeZone tz2 = DateTimeZoneProviders.Tzdb["America/New_York"];
ZonedDateTime zdt2 = zdt1.WithZone(tz2);

// If you need a DateTimeOffset, you can get one easily
DateTimeOffset dto = zdt2.ToDateTimeOffset();

lately I got another solution without using any other Lib. 最近我得到了另一个解决方案,而没有使用任何其他库。 In my case it has solved my problem. 就我而言,它解决了我的问题。 In my case, one timezone is fixed(ex my from timezone is UTC -4 and needed to convert it to systems local time) so i can take it to UTC time and then convert it to local timezone of my system, in this particular case wp sdk by using AddHours() method of DateTime . 就我而言,一个时区是固定的(例如,我来自时区的时区为UTC -4 ,需要将其转换为系统本地时间),因此在这种情况下,我可以将其转换为UTC时间,然后将其转换为系统的本地时区wp sdk通过使用DateTime的 AddHours()方法。 Here it is : 这里是 :

string GetLocalDateTime(DateTime targetDateTime)
{
    int fromTimezone = -3;
    int localTimezone;

    if (TimeZoneInfo.Local.BaseUtcOffset.Minutes != 0)
        localTimezone = Convert.ToInt16(TimeZoneInfo.Local.BaseUtcOffset.Hours.ToString() + (TimeZoneInfo.Local.BaseUtcOffset.Minutes / 60).ToString());
    else
        localTimezone = TimeZoneInfo.Local.BaseUtcOffset.Hours;

    DateTime Sdt = targetDateTime;
    DateTime UTCDateTime = targetDateTime.AddHours(-(fromTimezone));
    DateTime localDateTime = UTCDateTime.AddHours(+(localTimezone));

    return localDateTime.ToLongDateString() + " " + localDateTime.ToShortTimeString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM