다음을 통해 공유


Get the local server time for your Azure Website

All Azure Website server times are set to UTC. Therefore, if you execute the following line of C# code on an Azure Website, you will not get the local time.

 Label.Text = DateTime.Now.ToString();

You might have a need to get the date and time in local terms to support your application. This can be done by using the System.TimeZoneInfo class. This class exposes 2 helpful methods. The first useful method is named FindSystemTimeZoneById(). By passing the time zone id to the method you can get the TimeZoneInfo for the given Id. For example, if your Azure Website was hosted in West Europe and you wanted the time for Munich, Germany, then you could use the following code set shown below. 

 var cetZone = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time");         
 var cetTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, cetZone);         
 MunichTimeLabel.Text = cetTime.ToString();

 Using the above code, I created a test Azure Website hosted in the Brazil South Region and placed some example output on it.  The Azure Website can be viewed here.

When I started working on some examples for this article and the test Azure Website, my first thought was how to get the Id for the FindSystemTimeZoneById() method, I.e. “Central Europe Standard Time”).  I was able to get a list of them by using the second useful method for finding the local time on an Azure Web Site, named GetSystemTimeZones().   The GetSystemTimeZones() method returns a collection of time zones configured on the platform.  The time zones are configured in the registry as shown in Figure 1 at the Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones location. 

Figure 1, Azure Web Sites Time Zones

I used the following code segment to list out the configured time zones on the test Azure Website.

 ReadOnlyCollection<TimeZoneInfo> timeZones = TimeZoneInfo.GetSystemTimeZones();         
 GridViewTZ.DataSource = timeZones;         
 GridViewTZ.DataBind();

Summary

The default server time set on the Azure Website platform is UTC.  Some applications running on the platform may need local time instead.  Using the code segments in this article, a developer can find out which time zones are supported on the platform and easily convert the UTC to that time zone. 

UPDATE 6-MAY-2015: It is now possible to make an addition to the Web Apps APP SETTINGS as shown in Figure 2 to set the Web App to local time.  Add the TimeZoneInfo Id value to the WEBSITE_TIME_ZONE attribute, then, the DateTime.Now() method will return local time instead of the default GMT.

Figure 2, how to get Azure Web Apps on local time, instead of Default GMT

Comments

  • Anonymous
    October 15, 2014
    great, save me a lot of time, thank you!

  • Anonymous
    August 03, 2015
    Thank lot . :-)

  • Anonymous
    September 28, 2015
    Does this address DST?

  • Anonymous
    September 29, 2015
    It is my understanding that the ConvertTime() method does manage DST.  referencesource.microsoft.com

  • Anonymous
    March 11, 2016
    Tried to set it to "Eastern Standard Time" for Canada/Toronto area - didn't work at all

  • Anonymous
    March 13, 2016
    @Slava, not sure why it is not working for you, but it still works as expected on the test site linked from this site.  Perhaps try it using code instead of the App Settings, and if it still does not work you should place a comment in the Azure App Services Forum.

    • Anonymous
      April 08, 2016
      Did you SAVE your settings?I mention this, because at first it didn't work for me neither.Until I saw the SAVE button at the bottom of the page... :-)