4

I am developing a project in Xamarin Studio using C#. Its a .net PCL project and my profile is 78. My problem is, i am unable to convert a DateTime from UTC to specified timezone. I am using below code to convert DateTime from UTC to specified local TimeZone.

   DateTime dateTime = (TimeZoneInfo.ConvertTime (DateTime.SpecifyKind (DateTime.UtcNow, DateTimeKind.Utc), profile.TimeZone));

I am getting below exception

The Kind property of the dateTime parameter is DateTimeKind.Utc, but the sourceTimeZone parameter does not equal TimeZoneInfo.Utc.

In PCL TimeZoneInfo.ConvertTime doesn't have a parameter for specifying the TimeZoneInfo sourceTimeZone. It has only 2 overloads with below parameters.

ConvertTime(DateTime, TimeZoneInfo) & ConvertTime(DateTimeOffset, TimeZoneInfo)

TimeZoneInfo exist only to specify destination TimeZoneInfo.

Also it doesn't have TimeZoneInfo.ConvertTimeFromUtc, TimeZoneInfo.ConvertTimeToUtc Methods.

Please someone help me to fix this.

2
  • 2
    This is all entirely by design. Timezone conversions requires an operating system with a database that keeps track of the timezone rules across the world. Available on a desktop class machine, not available on limited devices like a phone. Without the database, you can only know something about UTC and the timezone for which the device was configured. You cannot use PCL if this is a requirement, using a commercial web service to make the conversion for you would be a workaround. Jun 12, 2014 at 8:55
  • I use nodatime.org in my Xamarin applications. Jun 7, 2016 at 13:37

1 Answer 1

5

To add to Hans's comment:

This is all entirely by design. Timezone conversions requires an operating system with a database that keeps track of the timezone rules across the world. Available on a desktop class machine, not available on limited devices like a phone. Without the database, you can only know something about UTC and the timezone for which the device was configured. You cannot use PCL if this is a requirement, using a commercial web service to make the conversion for you would be a workaround.

Have a look at Noda Time. This is a Date/Time library for .NET which has its own time zone data so it doesn't have to rely on the OS. It also supports PCLs.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.