TimeZone
This C# type, along with TimeZoneInfo
, describes the current time zone. It gets information about the time zone of the current computer.
Time zones on the planet Earth change on lines on longitude. You cannot assume a particular time zone. Handling TimeZones
is complex.
TimeZoneInfo
In newer versions of .NET, like .NET 7 in 2023, we get a warning that TimeZone
is obsolete. We should access time zones with the TimeZoneInfo
class
.
TimeZone
with TimeZoneInfo.Local
. Also Utc is a valid property.TimeSpan
by using the GetUtcOffset
method.using System; // Step 1: get current time zone. var zone = TimeZoneInfo.Local; Console.WriteLine(zone.DisplayName); // Step 2: get offset. (The hours change from UTC) TimeSpan offset = zone.GetUtcOffset(DateTime.Now); Console.WriteLine(offset); // Get utc. var zone2 = TimeZoneInfo.Utc; Console.WriteLine(zone2.DisplayName);(UTC-08:00) Pacific Time (Los Angeles) -07:00:00 (UTC) Coordinated Universal Time
What is the name of a specific time zone? We get information about the names for time zones. We can access the properties StandardName
and DaylightName
to get these strings.
TimeZone.CurrentTimeZone
property accessor.using System; // Get current time zone. TimeZone zone = TimeZone.CurrentTimeZone; string standard = zone.StandardName; string daylight = zone.DaylightName; Console.WriteLine(standard); Console.WriteLine(daylight);Mountain Standard Time Mountain Daylight Time
There are two concepts of time you can use. The local time depends on where the computer is located. The universal time is independent of this.
using System; TimeZone zone = TimeZone.CurrentTimeZone; // Demonstrate ToLocalTime and ToUniversalTime. DateTime local = zone.ToLocalTime(DateTime.Now); DateTime universal = zone.ToUniversalTime(DateTime.Now); Console.WriteLine(local); Console.WriteLine(universal);8/26/2010 1:02:28 PM 8/26/2010 7:02:28 PM
A UTC offset indicates how many hours a time zone differs from the Coordinated Universal Time. When you call GetUtcOffset
on a TimeZone
instance, you get a TimeSpan
instance.
TimeSpan
indicates how many hours the time is different from UTC.MeanTime
.using System; TimeZone zone = TimeZone.CurrentTimeZone; // Get offset. TimeSpan offset = zone.GetUtcOffset(DateTime.Now); Console.WriteLine(offset);-06:00:00
In this example, we get information about the current year's daylight saving changes. If you need to know when the Daylight Changes are, this method is ideal.
using System; using System.Globalization; // Get daylight saving information for current time zone. TimeZone zone = TimeZone.CurrentTimeZone; DaylightTime time = zone.GetDaylightChanges(DateTime.Today.Year); Console.WriteLine("Start: {0}", time.Start); Console.WriteLine("End: {0}", time.End); Console.WriteLine("Delta: {0}", time.Delta);Start: 3/14/2010 2:00:00 AM End: 11/7/2010 2:00:00 AM Delta: 01:00:00
The TimeZone
type provides a handy way to get information about time zones. We use the CurrentTimeZone
property to get the information for the computer.