Dealing With Time Zones Using TimeZoneInfo 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.

using System;
using System.Collections.ObjectModel;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();

            foreach (TimeZoneInfo zone in zones)
            {
                Console.WriteLine(zone.DisplayName);
            }
        }
    }
}

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.

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.

using System;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            // Display the current time zone name.
            Console.WriteLine("Local time zone: {0}\n", TimeZoneInfo.Local.DisplayName);
        }
    }
}

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.

using System;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            // Display the current time zone name.
            Console.WriteLine("Local time zone Name: {0}\n", TimeZoneInfo.Local.DisplayName);

            // Display the current time zone standard name.
            Console.WriteLine("Local time zone Standard Name: {0}\n", TimeZoneInfo.Local.StandardName);

            // Display the current time zone Daylight name.
            Console.WriteLine("Local time zone Daylight Name: {0}\n", TimeZoneInfo.Local.DaylightName);

            // Find whether the currently selected time zone supports daylight saving time.
            Console.WriteLine("Local time zone supports Daylight Saving Time: {0}\n", TimeZoneInfo.Local.SupportsDaylightSavingTime);
        }
    }
}

Output

display-the-differents-names-of-the-currently-selected-time-zone-using-c-sharp.jpg

Gets the time difference between Current TimeZone 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.

using System;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            // Display the current time zone name.
            Console.WriteLine("Local time zone Name: {0}\n", TimeZoneInfo.Local.DisplayName);

            // Apply base UTC offset
            TimeZoneInfo localZone = TimeZoneInfo.Local;
            Console.WriteLine("The {0} time zone is {1}:{2} {3} than Coordinated Universal Time.",
                localZone.StandardName,
                Math.Abs(localZone.BaseUtcOffset.Hours),
                Math.Abs(localZone.BaseUtcOffset.Minutes),
                (localZone.BaseUtcOffset >= TimeSpan.Zero) ? "later" : "earlier");
        }
    }
}

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.

using System;
using System.Collections.ObjectModel;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();
            Console.WriteLine("The local system has the following {0} time zones", zones.Count);
        }
    }
}

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.

using System;
using System.Collections.ObjectModel;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();
            Console.WriteLine("The local system has the following {0} time zones\n", zones.Count);
            
            foreach (TimeZoneInfo zone in zones)
            {
                Console.WriteLine(zone.Id);
            }
        }
    }
}

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#.


Similar Articles