This code snippet demonstrates how to get time difference between GMT and local time zone using TimeZone class in .NET.
The following code uses the TimeZone class to get the name of the current time zone, difference between GMT and local time zones, and also the details about day light saving time.
The TimeZone.CurrentTimeZone returns the current timezone of the current machine.
- const string dataFmt = "{0,-30}{1}";
- const string timeFmt = "{0,-30}{1:MM-dd-yyyy HH:mm}";
- TimeZone curTimeZone = TimeZone.CurrentTimeZone;
-
- Console.WriteLine( dataFmt, "TimeZone Name:", curTimeZone.StandardName );
-
- Console.WriteLine( dataFmt, "Daylight saving time?", curTimeZone.IsDaylightSavingTime( DateTime.Now ) );
-
- DateTime curUTC = curTimeZone.ToUniversalTime( DateTime.Now );
- Console.WriteLine( timeFmt, "Coordinated Universal Time:", curUTC );
-
- TimeSpan currentOffset = curTimeZone.GetUtcOffset( DateTime.Now );
- Console.WriteLine( dataFmt, "UTC offset:", currentOffset );
-
- System.Globalization.DaylightTime dl = curTimeZone.GetDaylightChanges( DateTime.Now.Year );
-
- Console.WriteLine("Start: {0:MM-dd-yyyy HH:mm} ", dl.Start);
-
- Console.WriteLine("End: {0:MM-dd-yyyy HH:mm} ", dl.End);
-
- Console.WriteLine("delta: {0}", dl.Delta );
- Console.Read();