Get Current Time Zone in C#

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.

const string dataFmt = "{0,-30}{1}";
const string timeFmt  = "{0,-30}{1:MM-dd-yyyy HH:mm}";
TimeZone curTimeZone = TimeZone.CurrentTimeZone;
// What is TimeZone name?
Console.WriteLine( dataFmt, "TimeZone Name:", curTimeZone.
StandardName );
// Is TimeZone DayLight Saving?|
Console.WriteLine( dataFmt, "Daylight saving time?", curTimeZone.IsDaylightSavingTime( DateTime.
Now ) );
// What is GMT (also called Coordinated Universal Time (UTC)
DateTime curUTC = curTimeZone.ToUniversalTime( DateTime.
Now );
.WriteLine( timeFmt, "Coordinated Universal Time:"
, curUTC );
// What is GMT/UTC offset ?
TimeSpan currentOffset = curTimeZone.GetUtcOffset( DateTime.
Now );
.WriteLine( dataFmt, "UTC offset:"
, currentOffset );
// Get DaylightTime object
System.Globalization.DaylightTime dl = curTimeZone.GetDaylightChanges( DateTime.Now.
Year );
// DateTime when the daylight-saving period begins.
Console.WriteLine("Start: {0:MM-dd-yyyy HH:mm} ", dl.
// DateTime when the daylight-saving period ends.
Console.WriteLine("End: {0:MM-dd-yyyy HH:mm} ", dl.
// Difference between standard time and the daylight-saving time.
Console.WriteLine("delta: {0}", dl.
Delta );

Up Next
    Ebook Download
    View all
    View all