Problem
Azure App Service uses UTC time by default for running web applications. This can pose challenges for older applications. For example, an application designed for Japanese users might have been developed with the expectation that it would always run on a server in Japan, which uses the Japan time zone. So, the application may use DateTime.Now
to retrieve the current time. When deployed to Azure App Service, this method returns UTC time, resulting in incorrect business data. This issue is common among many legacy applications developed before the era of cloud computing.
Since Azure App Service is a managed service, users cannot log into the server to change the time zone. However, this does not mean that we cannot run legacy applications on this PaaS service or that we need to fall back to using virtual machines. This article will guide you on how to change the time zone for Azure App Service.
Solution
First, let me demonstrate the problem. The following code displays current time, current time in UTC, and current time zone.
@page
@{
var timeZone = TimeZoneInfo.Local;
var timeZoneId = timeZone.Id;
var timeZoneDisplayName = timeZone.DisplayName;
var timeZoneStandardName = timeZone.StandardName;
}
DateTime.Now: @DateTime.Now <br />
DateTime.UtcNow: @DateTime.UtcNow <br />
Time Zone Id: @timeZoneId <br />
Time Zone Display Name: @timeZoneDisplayName <br />
Time Zone Standard Name: @timeZoneStandardName <br />
When it runs on Azure App Service, we can see it's showing UTC time.
Suppose this application is designed to function correctly only in Japan, where the time zone needs to be set to Tokyo Standard Time. We can achieve this by using an environment variable.
On the Azure portal, go to "Environment variables" blade under your App Service resource, and add a new environment variable with name of WEBSITE_TIME_ZONE
and value of Tokyo Standard Time
.
According to Microsoft, "By default, the time zone for the app is always UTC. You can change it to any of the valid values that are listed in Default Time Zones. If the specified value isn't recognized, UTC is used.".
The website will be restarted after saving the settings. Please wait a few minutes for it to restart.
Now our legacy application built for Japan time zone can get a correct time!
Comments