C# wp8c中的时区转换#

C# wp8c中的时区转换#,c#,.net,datetime,windows-phone-8,timezone,C#,.net,Datetime,Windows Phone 8,Timezone,我想知道没有办法把一个时区的日期和时间转换成另一个!! 我有很多关于时区转换的例子,都在非WP SDK上。令人惊讶的是,WP SDK的命名空间系统的类TimeZoneInfo没有方法查找SystemTimeZoneById() 这里和是在.net上转换时区的非常好的例子,除了WP SDK。 假设有一个场景,我在澳大利亚西部有一个时间标准时间,现在我需要将该时间转换为东部标准时间。通常我会这样做: string timeZoneStringOne = "W. Australia Standard

我想知道没有办法把一个时区的日期和时间转换成另一个!! 我有很多关于时区转换的例子,都在非WP SDK上。令人惊讶的是,WP SDK的命名空间系统的类TimeZoneInfo没有方法查找SystemTimeZoneById()

这里和是在.net上转换时区的非常好的例子,除了WP SDK。 假设有一个场景,我在澳大利亚西部有一个时间标准时间,现在我需要将该时间转换为东部标准时间。通常我会这样做:

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
*/

但是我如何在windows phone 8上执行此操作?

不幸的是,WP8和WinRT上的
TimeZoneInfo
对象被禁用,因为没有
FindSystemTimeZoneById
,甚至没有
Id
属性

依我看,最好的解决办法是改用。当编译为可移植类库(PCL)时,它可以在WP8上正常运行。PCL是最新NuGet软件包中的默认版本,因此您应该能够简单地使用它

但是,您需要使用而不是Microsoft的,因为BCL时区提供程序在便携式版本中不可用

// 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();

不幸的是,
TimeZoneInfo
对象在WP8和WinRT上被禁用,因为没有
FindSystemTimeZoneById
,甚至没有
Id
属性

依我看,最好的解决办法是改用。当编译为可移植类库(PCL)时,它可以在WP8上正常运行。PCL是最新NuGet软件包中的默认版本,因此您应该能够简单地使用它

但是,您需要使用而不是Microsoft的,因为BCL时区提供程序在便携式版本中不可用

// 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();

不幸的是,
TimeZoneInfo
对象在WP8和WinRT上被禁用,因为没有
FindSystemTimeZoneById
,甚至没有
Id
属性

依我看,最好的解决办法是改用。当编译为可移植类库(PCL)时,它可以在WP8上正常运行。PCL是最新NuGet软件包中的默认版本,因此您应该能够简单地使用它

但是,您需要使用而不是Microsoft的,因为BCL时区提供程序在便携式版本中不可用

// 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();

不幸的是,
TimeZoneInfo
对象在WP8和WinRT上被禁用,因为没有
FindSystemTimeZoneById
,甚至没有
Id
属性

依我看,最好的解决办法是改用。当编译为可移植类库(PCL)时,它可以在WP8上正常运行。PCL是最新NuGet软件包中的默认版本,因此您应该能够简单地使用它

但是,您需要使用而不是Microsoft的,因为BCL时区提供程序在便携式版本中不可用

// 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();

最近我得到了另一个解决方案,没有使用任何其他库。就我而言,它解决了我的问题。在我的例子中,一个时区是固定的(例如my from timezone是UTC-4,需要将其转换为系统本地时间),因此我可以将其转换为UTC时间,然后使用DateTimeAddHours()方法将其转换为系统的本地时区。这是:

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();
}

最近我得到了另一个解决方案,没有使用任何其他库。就我而言,它解决了我的问题。在我的例子中,一个时区是固定的(例如my from timezone是UTC-4,需要将其转换为系统本地时间),因此我可以将其转换为UTC时间,然后使用DateTimeAddHours()方法将其转换为系统的本地时区。这是:

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();
}

最近我得到了另一个解决方案,没有使用任何其他库。就我而言,它解决了我的问题。在我的例子中,一个时区是固定的(例如my from timezone是UTC-4,需要将其转换为系统本地时间),因此我可以将其转换为UTC时间,然后使用DateTimeAddHours()方法将其转换为系统的本地时区。这是:

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();
}

最近我得到了另一个解决方案,没有使用任何其他库。就我而言,它解决了我的问题。在我的例子中,一个时区是固定的(例如my from timezone是UTC-4,需要将其转换为系统本地时间),因此我可以将其转换为UTC时间,然后使用DateTimeAddHours()方法将其转换为系统的本地时区。这是:

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();
}

这需要一个时区信息数据库。你有一个在桌面上,它对你的裤子口袋来说太重了。没什么了不起的。使用web服务或保留自己的服务是一种方法。这需要一个时区信息数据库。你有一个在桌面上,它对你的裤子口袋来说太重了。没什么了不起的。使用web服务或保留自己的服务是一种方法。这需要一个时区信息数据库。你有一个在桌面上,它对你的裤子口袋来说太重了。没什么了不起的。使用web服务或保留自己的服务是一种方法。这需要一个时区信息数据库。你有一个在桌面上,它对你的裤子口袋来说太重了。没什么了不起的。使用web服务或保留自己的服务是一种方法。只需将
BaseUtcOffset
保留为
TimeSpan
即可消除所有逻辑和字符串操作。但即便如此,您也没有考虑本地时区的DST。此外,您可以在将其调整为UTC后,在
DateTime
上使用
ToLocalTime
。或者更好的方法是,只需从源值创建一个
DateTimeOffset
,然后对其使用
ToLocalTime
。我会把这当作一个答案,只是你最初的问题是关于在两个指定时区之间转换,而不是在固定输入和本地时区之间转换