6

I have a scenario in which I want to turn UTC dates into ISO 8601 strings with a certain timezone to send down via web api. The recommended way to do this is to use TimeZoneInfo like so:

var configuredTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneString);
var localTime = DateTime.SpecifyKind(TimeZoneInfo.ConvertTimeFromUtc(utcTime, configuredTimeZone), DateTimeKind.Local);
var stringResult = localTime.ToString("o");

This works fine on my local machine, but I'm running into a very weird situation where ToString outputs a different string when the code is hosted in an Azure web app. Locally I get 2017-02-20T00:00:00-06:00 (which is what I want, as it contains the timezone info I need), but when hosted in Azure I get 2017-02-20T00:00:00+00:00.(which is in UTC, not what I want). Since I am manually applying the time zone I want, I am not sure why the format is appending the wrong timezone information. Has anyone run into this before?

3
  • DateTime cannot store a time zone.
    – SLaks
    Feb 22, 2017 at 22:43
  • 4
    and the local timezone in Azure is UTC. Feb 22, 2017 at 22:44
  • I'm manually providing the timezone I want via TimeZoneInfo.ConvertTimeFromUtc, so how would Azure's use of UTC mess up the conversion?
    – Nathan G
    Feb 22, 2017 at 22:50

1 Answer 1

7

As has been mentioned in the comments, System.DateTime cannot store time zones. All it knows is "local" or "UTC". Being hosted in Azure, its "local" time is UTC.

So your statement of TimeZoneInfo.ConvertTimeFromUtc(utcTime, configuredTimeZone) would convert the UTC time to your time (midnight of the 20th), but because DateTime holds no timezone, it is of DateTimeKind.Unspecified. Your DateTime.SpecifyKind(..., DateTimeKind.Local) simply tells it that it is of type "local", and in the case of an Azure host, UTC+00:00.

If timezones are important, you want to utilise System.DateTimeOffset instead of DateTime. It is specifically built for holding and manipulating (as its name implies) timezone offsets.

Your Answer

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

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