0

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 ???

Community
  • 1
  • 1
Mushfiq
  • 759
  • 1
  • 8
  • 41
  • http://stackoverflow.com/questions/14230393/convert-datetime-to-another-timezone – Ulugbek Umirov May 27 '14 at 13:59
  • This requires a database of timezone info. You have one on a desktop, it is too heavy for your pant pocket. So nothing very amazing. Using a web service or keeping your own is an approach. – Hans Passant May 27 '14 at 15:05

2 Answers2

3

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();
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
0

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();
}
unknown6656
  • 2,765
  • 2
  • 36
  • 52
Mushfiq
  • 759
  • 1
  • 8
  • 41
  • You could eliminate all the logic and string manipulation by just keeping the `BaseUtcOffset` as a `TimeSpan`. But even then, you're not taking DST of the local time zone into account. – Matt Johnson-Pint Jun 09 '14 at 16:17
  • Also, you could simply use `ToLocalTime` on the `DateTime` after adjusting it to UTC. Or better yet, just create a `DateTimeOffset` from the source values and then use `ToLocalTime` on that. I'd put this as an answer, except that your original question was about converting between two *named* time zones, not between a fixed input and the local zone. – Matt Johnson-Pint Jun 09 '14 at 16:24