9

I am working on a project in which i am getting UTC DateTime in service response. I am trying to convert UTC DateTime to Datetime in specific time zone. i have tried System.TimeZoneInfo.ConvertTimeBySystemTimeZoneId but it is unavailable in xamarin forms pcl.

Is there any solution or workaround to handle this scenario?

Thanks

Sunny
  • 1,504
  • 14
  • 22
  • So do you already *have* the `TimeZoneInfo` that you want to convert into? More details would be helpful. – Jon Skeet Oct 11 '17 at 09:21
  • I am getting UTC DateTime after parsing the json from service. I am trying to change timezone by TimeZoneInfo.ConvertTimeBySystemTimeZoneId method but it is missing. There is TimeZoneInfo.Convert method is present. – Sunny Oct 11 '17 at 10:04
  • But what time zone are you trying to use? – Jon Skeet Oct 11 '17 at 10:10
  • US & Canada, Alaska, Gaza and many more. – Sunny Oct 11 '17 at 10:13
  • 3
    So what identifiers do you have for those? If they're Windows time zone IDs, they won't work on a non-Windows platform. If they're IANA time zone IDs (such as "Europe/London") then you *may* be able to get them to work using `TimeZoneInfo.FindSystemTimeZoneById`, but you might also want to use my Noda Time project. – Jon Skeet Oct 11 '17 at 10:42
  • I will Sir, Thanks – Sunny Oct 11 '17 at 10:45
  • This might be helpful for you/anyone: https://stackoverflow.com/a/59281255/4841439 – Divyesh Dec 11 '19 at 08:11
  • This question has answered here: https://stackoverflow.com/a/59281255/4841439 – Divyesh Dec 11 '19 at 09:02

2 Answers2

3

Try to use ToLocalDateTime() this way:

DateTime date = DateTime.Now;
var test = date.ToLocalDateTime();
Diego Rafael Souza
  • 5,241
  • 3
  • 23
  • 62
1

Try below code:

TimeZoneInfo.ConvertTime(date, TimeZoneInfo.Local);
Ziyad Godil
  • 2,620
  • 1
  • 14
  • 36