Mushfiq
Mushfiq

Reputation: 769

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. Amazingly TimeZoneInfo class of namespace System of WP SDK has no method for FindSystemTimeZoneById().

Here Converting time between Timezones and How to convert a datetime to specific timezone in c#? are very nice examples of converting timezones on .net except WP SDK. Suppose a scenario, I have a time in W. Australia Standard Time, now I need to convert that time to Eastern Standard Time. 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 ???

Upvotes: 0

Views: 734

Answers (2)

Mushfiq
Mushfiq

Reputation: 769

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

Upvotes: 0

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241848

Unfortunately, the TimeZoneInfo object is crippled on WP8 and WinRT, in that there is no FindSystemTimeZoneById, or even an Id property.

IMHO, the best solution is to use Noda Time instead. When compiled as a Portable Class Library (PCL), it can run on WP8 just fine. PCL is the default build in the latest NuGet package, so you should simply be able to use it.

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.

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

Upvotes: 3

Related Questions