Programming Field

Tzutil - DOS/Command Prompt Reference

[Windows Vista? or later] Displays or changes the time zone setting for the current user.

Note that changing the time zone requires the time zone changing privilege (‘SeTimeZonePrivilege’). By default, even regular users (Users group) have this privilege granted.

Syntax

Specifies one of the three options (+ corresponding arguments). If ‘/?’ is specified or nothing is specified, the command-line help will be displayed.

tzutil[.exe] /g | /s <time-zone-id>[_dstoff] | /l
/g
Outputs the current time zone setting.
/s
Changes the current time zone setting. <time-zone-id> will be the name of the target time zone setting. The name is one listed by ‘/l’ (case insensitive).
Additionally, if you want to disable daylight saving time (DST) adjustment, append ‘_dstoff’ (including the underscore) to the end of the time zone setting name when specifying it. (This is ignored for time zone settings that do not use daylight saving time.)
* You cannot use names from the tz database as the setting names. Additionally, the format ‘UTC-xx’ or ‘UTC+xx’ is only applicable to some time offsets.
* If the time zone setting name includes spaces, you need to enclose the entire name, including the ‘_dstoff’ part, in double quotation marks (‘" "’).
/l (‘l’ is lowercase L, ‘L’ can be used interchangeably for the ‘/l’ option)
Lists of available time zone settings in the system, outputted in descending order of the time difference from UTC, starting with the time zone with the largest negative offset and ending with the time zone with the largest positive offset.

Details

Tzutil is used when changing time zone settings from the command line. While time zone settings are not frequently changed, Tzutil can be utilized when automating configurations, such as including calls to Tzutil in batch files as part of the initial computer setup.

Tzutil returns an exit code upon completion of the command execution. If the exit code is 0, it indicates successful completion, while any other value suggests issues such as parameter errors. If no arguments are specified (resulting in the display of command-line help), the exit code will be a value other than 0. However, if ‘/?’ is specified, the exit code will be 0.

Samples

Sample 1

tzutil /s "Tokyo Standard Time"

Change the time zone setting to Japan Standard Time (UTC+09:00, with the display name ‘Osaka, Sapporo, Tokyo’).

Sample 2 (Batch file)

@echo off
setlocal enableextensions
for /F "delims=" %%A in ('tzutil /G') do set "TEMP_TZ=%%A"
tzutil /S UTC
rem -- format date and time
for /F "tokens=1,2,* delims=:" %%I in ("%TIME%") do set MYTIME=%%I%%J
set MYDATE=%DATE:/=%
set MYDATE=%MYDATE: =%
set UTC_DATE_TIME=%MYDATE%_%MYTIME%
endlocal & tzutil /S "%TEMP_TZ%" & set UTC_DATE_TIME=%UTC_DATE_TIME%

[Extensions] Obtains the current time in UTC and sets the date and time (hour-minute) to the environment variable ‘UTC_DATE_TIME’. To obtain UTC time, temporarily use Tzutil to set the time zone to UTC, retrieve the date and time via the special environment variables ‘DATE’ and ‘TIME’, format them, and set them to ‘UTC_DATE_TIME’. Then, revert the time zone setting back to its original state using Tzutil.

In this batch file, the For command is used to store the current time zone setting in an environment variable. Additionally, the Setlocal command to Endlocal command, along with the ‘&’ character, is employed to create temporary environment variables and export them outside the batch file.

* Changing the time zone setting with Tzutil is not affected by the localization done with Setlocal; therefore, it needs to be manually reverted. Additionally, if the time zone setting is changed by another operation during the execution of this batch file, there is a possibility that the setting may be overwritten depending on the timing (however, since the time zone setting change itself takes only a few seconds, it is unlikely to pose a significant issue in most cases).