Home
C#
TimeZone, TimeZoneInfo Examples
This page was last reviewed on May 6, 2023.
Dot Net Perls
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.
DateTime
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.
Step 1 We can access the system's local TimeZone with TimeZoneInfo.Local. Also Utc is a valid property.
Property
Step 2 We can compute the offset as a 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
Name. 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.
Also This example shows how to get the current time zone with the 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
Universal 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.
Tip With universal times, you can compare times from different locations with no errors.
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
Get offsets. 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.
And The returned TimeSpan indicates how many hours the time is different from UTC.
TimeSpan
Note Coordinated Universal Time is also referred to as Zulu Time or Greenwich MeanTime.
using System; TimeZone zone = TimeZone.CurrentTimeZone; // Get offset. TimeSpan offset = zone.GetUtcOffset(DateTime.Now); Console.WriteLine(offset);
-06:00:00
Daylight changes. 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
A summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on May 6, 2023 (new example).
Home
Changes
© 2007-2024 Sam Allen.