Dealing With Time Zones Using TimeZoneInfo In C#

In this article, I will explain how to perform various time zone related operations using TimeZoneInfo class in C#.

Introduction

 
In my previous article, I explained what TimeZone is and how to display all local system Time Zones in ASP.Net using C#. Now I will explain various time zone properties and how to use TimeZoneInfo class in C# to display all the information about time zones.
 
Create a console application in Visual Studio. We will use console app to test our code.
 

Display all the TimeZone Names of local system

 
To display all time zones in our current system, we use TimeZoneInfo.GetSystemTimeZone() static method that returns all available time zones on a machine. The DisplayName property is the name of the time zone. 
  1. namespace ConsoleApplication6  
  2. {  
  3. class Program  
  4. {  
  5. static void Main(string[] args)  
  6. {  
  7. ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();  
  8. foreach (TimeZoneInfo zone in zones)  
  9. {  
  10. Console.WriteLine(zone.DisplayName);  
  11. }  
  12. }  
  13. }  
  14. }  
Output
 
Display-all-time-zone-of-local-system-using-c-sharp.jpg 

Display all the TimeZones that do not support DayLightSavingTime 

For this, I use the SupportsDaylightSavingTime property of the TimeZoneInfo class. I apply the concept that if this property is not supported then display all the TimeZone names of the current system. Here is the complete code.
  1. namespace ConsoleApplication6  
  2. {  
  3. class Program  
  4. {  
  5. static void Main(string[] args)  
  6. {  
  7. ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();  
  8. Console.WriteLine("The TimeZones that not support the DayLightSavingTime :");  
  9. foreach (TimeZoneInfo zone in zones)  
  10. {  
  11. if (!zone.SupportsDaylightSavingTime)  
  12. Console.WriteLine(zone.DisplayName);  
  13. }  
  14. }  
  15. }  
  16. }  
Output 
 
display-all-the-time-zone-that-not-support-daylightsavingtime-in-c-sharp.jpg 

Local TimeZone Information 

 
All the local TimeZone information (currently selected TimeZone in our local system) can be indentified by using the Local Property of the TimeZoneInfo class.
 

Get the name of the currently selected TimeZone in our local system

 
To display the name of the TimeZone which is currently set on our system, we use the DisplayName property of the Local property of the TimeZoneInfo class. 
  1. namespace ConsoleApplication6  
  2. {  
  3. class Program  
  4. {  
  5. static void Main(string[] args)  
  6. {  
  7. // Display the current time zone name.  
  8. Console.WriteLine("Local time zone: {0}\n", TimeZoneInfo.Local.DisplayName);  
  9. }  
  10. }  
  11. }  
Output
 
Display-the-currently-selected-timezone-in-our-system-using-c-sharp.jpg
 

Get the Standard Name, DayLight Name of TimeZones

 
To display other information about the currently selected TimeZone on our local system, we use other properties. For example, to find the Standard name, we use the StandardName property, for the DayLight Name, we use the DaylightName property. In the following example I also show whether the currently selected TimeZone supports DaylightSavingTime or not; if yes then it gives true, if not then it gives false.
  1. namespace ConsoleApplication6  
  2. {  
  3. class Program  
  4. {  
  5. static void Main(string[] args)  
  6. {  
  7. // Display the current time zone name.  
  8. Console.WriteLine("Local time zone Name : {0}\n", TimeZoneInfo.Local.DisplayName);  
  9. //Display the current time zone standard name.  
  10. Console.WriteLine("Local time zone Standard Name : {0}\n", TimeZoneInfo.Local.StandardName);  
  11. //Display the current time zone Daylight name.  
  12. Console.WriteLine("Local time zone DayLight Name : {0}\n", TimeZoneInfo.Local.DaylightName);  
  13. //Find whether the currenctly selected Time zone support the day light saving time or not  
  14. Console.WriteLine("Local time zone support Day Light Saving Time : {0}\n", TimeZoneInfo.Local.SupportsDaylightSavingTime);  
  15. }  
  16. }  
  17. }  
Output
 
display-the-differents-names-of-the-currently-selected-time-zone-using-c-sharp.jpg 

Gets the time difference between Current TimeZone and and Coordinated Universal Time (UTC) 

To find the difference between the current time zone's standard time and the UTC, we use the BaseUtcOffset property of the TimeZoneInfo class.
  1. namespace ConsoleApplication6  
  2. {  
  3. class Program  
  4. {  
  5. static void Main(string[] args)  
  6. {  
  7. // Display the current time zone name.  
  8. Console.WriteLine("Local time zone Name : {0}\n", TimeZoneInfo.Local.DisplayName);  
  9. //apply base UTC offset  
  10. TimeZoneInfo localZone = TimeZoneInfo.Local;  
  11. Console.WriteLine("The {0} time zone is {1}:{2} {3} than Coordinated Universal Time.",  
  12. localZone.StandardName,  
  13. Math.Abs(localZone.BaseUtcOffset.Hours),  
  14. Math.Abs(localZone.BaseUtcOffset.Minutes),  
  15. (localZone.BaseUtcOffset >= TimeSpan.Zero) ? "later" : "earlier");  
  16. }  
  17. }  
  18. }  
Output
 
display-difference-between-the-currently-selected-timezone-with-utc-using-c-sharp.jpg


Determine the total number of TimeZones that our system supports 

To find the total number of TimeZones that are present in our system we use the count property.
  1. namespace ConsoleApplication6  
  2. {  
  3. class Program  
  4. {  
  5. static void Main(string[] args)  
  6. {  
  7. ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();  
  8. Console.WriteLine("The local system has the following {0} time zones", zones.Count);  
  9. }  
  10. }  
  11. }  
Output
 
Display-number-of-timezone-in-the-current-system-using-c-sharp.jpg


Display all the current system's TimeZones using the ID property 

We can also display the TimeZone by using the Id.

  1. namespace ConsoleApplication6  
  2. {  
  3. class Program  
  4. {  
  5. static void Main(string[] args)  
  6. {  
  7. ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();  
  8. Console.WriteLine("The local system has the following {0} time zones \n", zones.Count);  
  9. foreach (TimeZoneInfo zone in zones)  
  10. Console.WriteLine(zone.Id);  
  11. }  
  12. }  
  13. }  
Output
 
Display-all-the-time-zone-using-id-in-c-sharp.jpg

Summary 

In this article, I explain how to perform various operations on time zones using C#.