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.
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.
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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on May 6, 2023 (new example).