Get Current Time Zone in VB.NET

This code snippet demonstrates how to get time difference between GMT and local time zone using TimeZone class in .NET.
  • 4663

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.

Sub GetCurrentTimeZone()

 

        Dim dataFmt As String = "{0,-30}{1}"

        Dim timeFmt As String = "{0,-30}{1:MM-dd-yyyy HH:mm}"

        Dim curTimeZone As TimeZone = 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)

        Dim curUTC As DateTime = curTimeZone.ToUniversalTime(DateTime.Now)

        Console.WriteLine(timeFmt, "Coordinated Universal Time:", curUTC)

        ' What is GMT/UTC offset ?

        Dim currentOffset As TimeSpan = curTimeZone.GetUtcOffset(DateTime.Now)

        Console.WriteLine(dataFmt, "UTC offset:", currentOffset)

        ' Get DaylightTime object

        Dim dl As System.Globalization.DaylightTime = curTimeZone.GetDaylightChanges(DateTime.Now.Year)

        ' DateTime when the daylight-saving period begins.

        Console.WriteLine("Start: {0:MM-dd-yyyy HH:mm} ", dl.Start)

        ' DateTime when the daylight-saving period ends.

        Console.WriteLine("End: {0:MM-dd-yyyy HH:mm} ", dl.End)

        ' Difference between standard time and the daylight-saving time.

        Console.WriteLine("delta: {0}", dl.Delta)

 

        Console.ReadKey()

    End Sub

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.