VB.NET TimeZone ExampleUse the TimeZone.CurrentTimeZone property and ToLocalTime and ToUniversalTime.
TimeZone. Day and night come in different phases across the planet. We have time zones so that no one needs to wake up at midnight local time.
With TimeZone.CurrentTimeZone, we can access the current time zone information on the system. This depends on the system's configuration. We can also use conversions based on the time zone.
Here we get the current time zone with TimeZone.CurrentTimeZone, a shared property in VB.NET. We see the computer is located in the Pacific Standard Time zone.
StandardName: The name of the current time zone (here it is Pacific Standard Time, PST).
DaylightName: The Day light Changes time zone name—so we know more about when the time changes each year.
VB.NET program that uses TimeZone, ToUniversalTime
Dim zone As TimeZone = TimeZone.CurrentTimeZone
' Get standard and day light names.
Dim standard As String = zone.StandardName
Dim daylight As String = zone.DaylightName
Console.WriteLine("STANDARD NAME: " + standard)
Console.WriteLine("DAYLIGHT NAME: " + daylight)
' Get local and universal time for DateTime.Now.
Dim local As DateTime = zone.ToLocalTime(DateTime.Now)
Dim universal As DateTime = zone.ToUniversalTime(DateTime.Now)
Console.WriteLine("LOCAL TIME: " + local)
Console.WriteLine("UNIVERSAL TIME: " + universal)
STANDARD NAME: Pacific Standard Time
DAYLIGHT NAME: Pacific Daylight Time
LOCAL TIME: 7/7/2018 7:11:49 AM
UNIVERSAL TIME: 7/7/2018 2:11:49 PM
Notes, ToLocalTime. We call ToLocalTime to convert a DateTime into the local time zone. In this program, the local time was the same as DateTime.Now, as Now returned a local time.
Notes, ToUniversalTime. This takes a DateTime and converts it to a universal time—one that is not based on local time. A new DateTime is returned.
Day light changes, and time zones, are a complex subject. But with the TimeZone.CurrentTimeZone property in VB.NET we gain insight into how dates and times are affected.DateTime
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to firstname.lastname@example.org.