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. 
 
The TimeZone.CurrentTimeZone returns the current timezone of the current machine. 
  1. const string dataFmt = "{0,-30}{1}";  
  2. const string timeFmt = "{0,-30}{1:MM-dd-yyyy HH:mm}";  
  3. TimeZone curTimeZone = TimeZone.CurrentTimeZone;  
  4. // What is TimeZone name?  
  5. Console.WriteLine( dataFmt, "TimeZone Name:", curTimeZone.StandardName );  
  6. // Is TimeZone DayLight Saving?|  
  7. Console.WriteLine( dataFmt, "Daylight saving time?", curTimeZone.IsDaylightSavingTime( DateTime.Now ) );  
  8. // What is GMT (also called Coordinated Universal Time (UTC)  
  9. DateTime curUTC = curTimeZone.ToUniversalTime( DateTime.Now );  
  10. Console.WriteLine( timeFmt, "Coordinated Universal Time:", curUTC );  
  11. // What is GMT/UTC offset ?  
  12. TimeSpan currentOffset = curTimeZone.GetUtcOffset( DateTime.Now );  
  13. Console.WriteLine( dataFmt, "UTC offset:", currentOffset );  
  14. // Get DaylightTime object  
  15. System.Globalization.DaylightTime dl = curTimeZone.GetDaylightChanges( DateTime.Now.Year );  
  16. // DateTime when the daylight-saving period begins.  
  17. Console.WriteLine("Start: {0:MM-dd-yyyy HH:mm} ", dl.Start);  
  18. // DateTime when the daylight-saving period ends.  
  19. Console.WriteLine("End: {0:MM-dd-yyyy HH:mm} ", dl.End);  
  20. // Difference between standard time and the daylight-saving time.  
  21. Console.WriteLine("delta: {0}", dl.Delta );  
  22. Console.Read();  


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.