TimeZone
and TimeZoneInfo
You might often ask what people are doing on the other side of world when in your country it’s a particular time of the day. Working with time zones can also be important for your business if you need to contact people who live in different and far away countries. The .NET Framework provides two types, TimeZone
and TimeZoneInfo
, which enable retrieving information on time zones. Both types are exposed by the System
namespace. For example, imagine you want to retrieve information on the time zone of your country. This can be accomplished as follows (assuming regional settings on your machine are effectively related to your country):
Dim zone As TimeZone = TimeZone.CurrentTimeZone
TimeZone
is a reference type and through its CurrentTimeZone
property it provides a lot of information such as the name of the time zone or the daylight-saving time period as demonstrated here:
This code produces the following result on my machine:
The official MSDN documentation states that using the TimeZoneInfo
class should be preferred instead of TimeZone
. This is because TimeZoneInfo
also provides the ability of creating custom time zones. The following code shows how you can retrieve current time zone information using TimeZoneInfo
:
Creating a custom time zone is also a simple task, which is accomplished by the following code:
All you need is specifying a custom identifier, the difference between the UTC time span and the local time span, a daylight identifier, and a standard identifier. TimeZoneInfo
also provides another useful method for enumerating time zones recognized by the system, named GetSystemTimeZones
and that you can use like this:
An excerpt of the output provided by this simple iteration is the following:
Thanks to this information, you could use the TimeZoneInfo
class for converting between time zones. The following code demonstrates how to calculate the hour difference between Italy and Redmond, WA:
Invoking the ConvertTimeBySystemZoneId
method, you can convert between your local system time and another time, based on the zone ID. If you don’t know zone IDs, just replace the previous iterations for showing the content of the timez.Id
property instead of DisplayName
.
18.222.196.175