Introduction
In my previous article I explained what a TimeZone is and how to display all the local system TimeZones in ASP.Net using C#. Now I explain how to display the TimeZone information and perform various operation on it in only C#. To see how it works let's use the following concepts. Open Visual Studio 2010 and click on "File" -> "New" -> "Project...". A window is opened; select Console application in it and give the name of your application that you want to give and then click ok.
Display all the TimeZone Names of local system
Now to display all the TimeZones in our current system we create the object of the TimeZoneInfo class and by using its DisplayName property we display all the names of the Current system's TimeZonns.
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 the TimeZone Names of local System that does 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. (The current system describes the system in which you are working.) For this write the code as:
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();
Console.WriteLine("The TimeZones that not support the DayLightSavingTime :");
foreach (TimeZoneInfo zone in zones)
{
if (!zone.SupportsDaylightSavingTime)
Console.WriteLine(zone.DisplayName);
}
}
}
}
Output
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 selected in our system, we use the DisplayName property of the Local property of the TimeZoneInfo class. Write the code as:
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
Get the Standard Name, DayLight Name, of the Currently selected TimeZone In Our local System
Now to display the other information about the currently selected TimeZone in our local system, we use the other properties. For example, to find the Standard name we use the StandardName property, for the DayLight Name we use the DaylightName property. Since we find all this information of the TimeZone that is currently selected so we mention the local property also. 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.
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 currenctly selected Time zone support the day light saving time or not
Console.WriteLine("Local time zone support Day Light Saving Time : {0}\n", TimeZoneInfo.Local.SupportsDaylightSavingTime);
}
}
}
Output
Gets the time difference between the current time zone's standard time 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 as:
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
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 as:
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 all the current system's TimeZones using the ID property
We can also display the TimeZone by using the Id as:
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
Summary
In this article I explain how to perform various operations on Timezones, by explaining many properties of them.