ARTICLE

Get Current Time Zone in VB.NET

Posted by Mahesh Chand Articles | VB.NET How do I August 01, 2010
This code snippet demonstrates how to get time difference between GMT and local time zone using TimeZone class in .NET.
 
Reader Level:

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

 

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor