WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Get UTC and Local Time

 Keywords: UTC Local Time TimeYmdHms WMI Win32_LocalTime Win32_UTCTime WQL

;Winbatch 2011A - UDF for UTC and Local time settings
;                 uses WMI queries
;
;Stan Littlefield, August 16, 2011
;////////////////////////////////////////////////////////////////////////////////////////

#DefineFunction getTime(format,nDelim,WBcvt,local)
;format: 1 for MM/DD/YYYY, any other number for DD/MM/YYYY
;nDelim: 1 or 2
;WBcvt: if @true convert to Winbatch date
;local: if @True generate local time, not UTC
delim=StrSub("/-",nDelim,1)
oWMI = GetObject("winmgmts:\\":".":"\root\CIMV2")
If local
  oColl = oWMI.ExecQuery("SELECT * FROM Win32_LocalTime", "WQL",48)
Else
  oColl = oWMI.ExecQuery("SELECT * FROM Win32_UTCTime", "WQL",48)
EndIf
ForEach oItem In oColl
   ;NOTE: milliseconds is not implemented
   If format == 1     ;US Time Format
      UTC = oItem.Month:delim:oItem.Day:delim:oItem.Year:" ":oItem.Hour:":":oItem.Minute:":":oItem.Second
   Else
      UTC = oItem.Day:delim:oItem.Month:delim:oItem.Year:" ":oItem.Hour:":":oItem.Minute:":":oItem.Second
   EndIf
Next
oColl=0
oWMI=0
If WBcvt Then UTC=ObjectType("DATE",UTC)
Return(UTC)
#EndFunction

;tests
time="getTime(1,2,@true,@false)   =>  ":getTime(1,2,@TRUE,@FALSE):@CRLF
time=time:"getTime(1,2,@false,@false)   =>  ":getTime(1,2,@FALSE,@FALSE):@CRLF
time=time:"getTime(1,1,@false,@false)   =>  ":getTime(1,1,@FALSE,@FALSE):@CRLF
time=time:"getTime(2,1,@false,@false)   =>  ":getTime(2,1,@FALSE,@FALSE):@CRLF
time=time:"getTime(1,1,@true,@true)   =>  ":getTime(1,1,@TRUE,@TRUE):@CRLF
time=time:"getTime(1,1,@false,@true)   =>  ":getTime(1,1,@FALSE,@TRUE):@CRLF

Message("Times",time)

Exit

Article ID:   W18306
Filename:   Get UTC and Local Time.txt
File Created: 2011:08:16:07:52:08
Last Updated: 2011:08:16:07:52:08