143} How do I get the current time zone in a script?
@echo off & setlocal enableextensions
::
:: Get the time zone in minutes
for /f "tokens=5" %%a in (
'w32tm /tz^|find "Time zone"') do set TZbias=%%a
for /f "tokens=1 delims=m" %%a in (
'echo %TZbias%') do set TZbias=%%a
::
:: Display the result in minutes
echo Bias: %TZbias% minutes
::
:: Display the result in hours
set /a TZfrac=%TZbias%
%%60
*100
/60
if %TZfrac% LSS 0 set /a TZfrac=-%TZfrac%
set /a TZbias=-%TZbias%/60
echo UTC+(%TZbias%.%TZfrac%)
endlocal & goto :EOF
The output for my own computer's time zone (Europe/Helsinki) is
C:\_D\TEST>cmdfaq
Bias: -120 minutes
UTC+(2.0)
Alternatively, in XP and beyond
@echo off & setlocal enableextensions
for /f "tokens=* skip=1" %%i in ('wmic OS Get CurrentTimeZone') do (
set CurrentTimeZone_=%%i)
echo CurrentTimeZone: %CurrentTimeZone_% minutes
endlocal & goto :EOF
The output for my own computer's time zone (Europe/Helsinki) in the
summer is
C:\_D\TEST>cmdfaq
CurrentTimeZone: 180 minutes
Another method:
@echo off & setlocal enableextensions
for /f "tokens=2,*" %%i in ('systeminfo^|findstr /b "Time Zone:"') do (
set tz_=%%j)
echo %tz_%
for /f "tokens=1" %%i in ('echo "%tz_%"') do set tz_=%%~i
set tz_=%tz_:(=%
set tz_=%tz_:)=%
echo %tz_%
endlocal & goto :EOF
The output might be e.g.
C:\_D\TEST>cmdfaq
(GMT+02:00) Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius
GMT+02:00
Using
Java
Script in WSH
@echo off & setlocal enableextensions
set js_=%temp%\tmp$$$.js
>"%js_%" echo WScript.Echo(new Date().getTimezoneOffset());
for /f %%a in ('cscript //nologo "%js_%"') do (
set TZbias=%%a)
for %%f in ("%js_%") do if exist %%f del "%%f"
echo TZbias=%TZbias%
endlocal & goto :EOF
The output for my own computer's time zone (Europe/Helsinki) in the
summer is
C:\_D\TEST>cmdfaq
TZbias=-180
A Visual Basic Script (VBScript) aided command line script solution
@echo off & setlocal enableextensions
::
:: Build a Visual Basic Script and run it
set vbs_=%temp%\tmp$$$.vbs
set skip=
findstr "'%skip%VBS" "%~f0" > "%vbs_%"
for /f "tokens=1-2" %%a in ('cscript //nologo "%vbs_%"') do (
set TZbias=%%a
set TZdaylightBias=%%b)
::
:: Clean up
for %%f in ("%vbs_%") do if exist %%f del %%f
::
:: Display the time zone offsets from Greenwich mean time
echo TZbias=%TZbias%
echo TZdaylightBias=%TZdaylightBias%
::
endlocal & goto :EOF
'
'The Visual Basic Script 'VBS
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 'VBS
Set colTimeZone = objWMIService.ExecQuery("Select * from Win32_TimeZone") 'VBS
For Each objTimeZone in colTimeZone 'VBS
Wscript.Echo objTimeZone.Bias & " " & objTimeZone.DaylightBias 'VBS
Next 'VBS
The output for my own computer's time zone (Europe/Helsinki) in the
summer is
C:\_D\TEST>cmdfaq
TZbias=120
TZdaylightbBias=-60
Caveat:
The item delves into whatever information the XP computer offers
under the hood. The interpretation of the terminology and the usage of
the results is up to the user, since mine is a scripting FAQ, not a
calendar intricacies treatise.