Determine my time zone offset using VBScript?

有些话、适合烂在心里 提交于 2019-11-28 04:19:48

问题


How can I determine my time zone offset using VBScript?

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


回答1:


Here is a revised function that appears to account 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

[Original function that does NOT account for Daylight Saving Time.]

Here is my answer to my question (original source).

For Eastern Standard Time (New York), this VBScript function will return -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


来源:https://stackoverflow.com/questions/13980541/determine-my-time-zone-offset-using-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!