How can I determine the timezone offset using VBScript?

How can I determine my timezone offset using VBScript ?

Windows provides an environment variable TZ. For Eastern Standard Time (New York), its value is equal EST5EDT. However, I am looking for a signed integer offset from UTC. (This value is -5 for Eastern Standard Time.)

+4
source share
1 answer

Here is a revised function that seems to allow for daylight saving time. (Inspired by this SO question .)

Function GetTimeZoneOffset()
    Const sComputer = "."

    Dim oWmiService : Set oWmiService = _
        GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
                  & sComputer & "\root\cimv2")

    Set cItems = oWmiService.ExecQuery("SELECT * FROM Win32_ComputerSystem")

    For Each oItem In cItems
        GetTimeZoneOffset = oItem.CurrentTimeZone / 60
        Exit For
    Next
End Function

[ , .]

( ).

(-) VBScript -5:

Function GetTimeZoneOffset()
    Const sComputer = "."

    Dim oWmiService : Set oWmiService = _
        GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
                  & sComputer & "\root\cimv2")

    Dim cTimeZone : Set cTimeZone = _
        oWmiService.ExecQuery("Select * from Win32_TimeZone")

    Dim oTimeZone
    For Each oTimeZone in cTimeZone
        GetTimeZoneOffset = oTimeZone.Bias / 60
        Exit For
    Next
End Function
+8

All Articles