Monday 21 February 2011

vbscript determine local timezone and UTC time

I've found it quite hard in the past to find script snippets on net for vbscript when you need to convert between UTC and local time in vbscript.

This has been useful for me in scripts that access database data that's stored in UTC (for example MOM & SCOM).

' UTC and dd mm yyyy vbscript code example

' Rick Jury

' ShowUTCTimeNow = Vbscript code to extract a UTC time for local machine using WMI

' DateRegion = show if dd/mm/yyyy or not in regional settings

' Also shows how to calculate the time offset from local time vs UTC.

 

' I couldn't find any working examle of how to make vbscript show a UTC time instead of local time so wrote one using WMI.

' note - ActiveTimeBias used in some code doesn't work on my vista machine as its a big dword.

 

' call function to show my utc time not local time.

MyUTCTime = ShowUTCTimeNow

wscript.echo "local UTC time is: " & MyUTCTime

 

' call a function to show dd mm yyyy format

MyFormat = DateRegion

wscript.echo "d m y format locally is: " & MyFormat

 

' Now how to figure out how many hours we currently off UTC allowing regardless incliding daylight savings

' returns difference of utc to now to HourOffset

wscript.echo "Current Offset in hours is: " & DateDiff("h",MyUTCTime,now)

 

Function ShowUTCTimeNow ()

                ' returns a string representing current UTC time but represented in typical vbscript acceptable date format 

 

                Const wbemFlagReturnImmediately = &h10

                Const wbemFlagForwardOnly = &h20

 

   Set objWMIService = GetObject("winmgmts:\\" & "." & "\root\CIMV2")

   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_UTCTime", "WQL", _

                                                                                                                                                                  wbemFlagReturnImmediately + wbemFlagForwardOnly)

 

                For Each objItem In colItems

     ' WScript.Echo "Day: " & objItem.Day

'      WScript.Echo "DayOfWeek: " & objItem.DayOfWeek

'      WScript.Echo "Hour: " & objItem.Hour

'      WScript.Echo "Milliseconds: " & objItem.Milliseconds

'      WScript.Echo "Minute: " & objItem.Minute

'      WScript.Echo "Month: " & objItem.Month

'      WScript.Echo "Quarter: " & objItem.Quarter

'      WScript.Echo "Second: " & objItem.Second

'      WScript.Echo "WeekInMonth: " & objItem.WeekInMonth

'      WScript.Echo "Year: " & objItem.Year

                                If DateRegion = "US" Then

                                                ShowUTCTimeNow = objItem.Month & "/" & objItem.Day & "/" &objItem.Year & " " & objItem.Hour & ":"&objItem.Minute & ":" & objItem.Second

                                Else

                                                ShowUTCTimeNow = objItem.Day & "/" & objItem.Month & "/" &objItem.Year & " " & objItem.Hour & ":"&objItem.Minute & ":" & objItem.Second

                                End If

   Next

 

End Function

 

Function DateRegion()

                ' This function identifies if you are running mm/dd/yyyy or dd/mm/yyyy

                ' knowing this is critical if you are converting string to date etc.

                If DatePart("D","12/06/2007") = "12" Then

                                DateRegion= "NZ"

                Else

                                DateRegion= "US"

                End If

End Function

No comments:

Post a Comment