Mashhad Saleem
Mashhad Saleem

Reputation: 177

Azure web app timezone error in asp.net webapi

I have web app in azure paas environment. I need to convert the time in different timezone, i have following code which run perfectly fine on dev machine but when i deploy on azure paas environment it throw error

TimeZoneInfo serverTimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(TimeZone.CurrentTimeZone.StandardName);
                return TimeZoneInfo.ConvertTimeToUtc(_lastUpdatedDate.Value, serverTimeZoneInfo);

First line throw exception. Error getting value from 'DateCreated' on 'ViewModels.Orders.OrderActivityViewModel'

Upvotes: 1

Views: 794

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241475

A few things:

  • Don't use the TimeZone class. It is obsolete. Use only the TimeZoneInfo class.
  • The StandardName is NOT the same as the Id for all cases. It is also impacted by OS language, where the Id is not.
  • The local time zone's Id is at TimeZoneInfo.Local.Id, though there's no point in finding it by Id when you already have it. You'd just use TimeZoneInfo.Local.
  • If you're just converting local time to UTC, you don't need to mess with time zones at all, just use .ToUniversalTime() on your original DateTime or DateTimeOffset value.
  • The server's time zone probably already is UTC. That is the default in Azure anyway. Thus it isn't going to do anything at all.
  • If necessary, you can change Azure App Service's WEBSITE_TIME_ZONE setting (on Windows only), but I don't recommend that. If you have a specific time zone in mind, then you would pass that ID into TimeZoneInfo.FindSystemTimeZoneById.
  • As others pointed out, the error you reported isn't in the code you gave, so likely none of this is going to help you.

Upvotes: 1

Related Questions