Recipe 2.7. Setting the System Time, Date, and Time ZoneProblemYou want to set the system time, date, and time zone. SolutionUsing a graphical user interface
Using a command-line interfaceThe following commands set the time to 11:02 p.m. and the date to November 1, 2005: > time 23:02:00 > date 11/01/2005 The date format may vary depending on your locale. Run this command to display the current date, time, and time zone: > date /t & time /t & w32tm -tz You can use this command to display time zone information from the registry: > reg query \\<ServerName>\HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation This command sets the SNTP server list: > net time /setsntp:<ServerList> For example: > net time /setsntp:mytime.rallencorp.com,time.windows.com This command queries the SNTP server: > net time /querysntp This command forces the local system to sync its time from the time source: > net time /set Using VBScript' This code displays the local date, time, and time zone on a target computer ' ------ SCRIPT CONFIGURATION ------ strComputer = "." ' e.g. rallen-srv01 ' ------ END CONFIGURATION --------- WScript.Echo "Current time using Now function: " WScript.Echo vbTab & Now set dicDaysOfWeek = CreateObject("Scripting.Dictionary") dicDaysOfWeek.Add 0, "Sun" dicDaysOfWeek.Add 1, "Mon" dicDaysOfWeek.Add 2, "Tue" dicDaysOfWeek.Add 3, "Wed" dicDaysOfWeek.Add 4, "Thu" dicDaysOfWeek.Add 5, "Fri" dicDaysOfWeek.Add 6, "Sat" set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set objDateTime = objWMI.Get("Win32_Localtime=@") WScript.Echo "Current time using WMI: " WScript.Echo vbTab & dicDaysOfWeek.Item(objDateTime.DayOfWeek) & " " & _ objDateTime.Month & "/" & objDateTime.Day & "/" & _ objDateTime.Year & " " & objDateTime.Hour & ":" & objDateTime.Minute WScript.Echo "Time zone:" set colTZ = objWMI.ExecQuery("select * from Win32_TimeZone") for each objTZ in colTZ Wscript.Echo vbTab & objTZ.Caption next The easiest way to set the time and date via a script is by shelling out to the time and date commands, but this means the script must be run locally. Here is an example: WScript.Echo "Current time: " & Now strCommand = "cmd.exe /c time 23:02:00" set objWshShell = WScript.CreateObject("WScript.Shell") intRC = objWshShell.Run(strCommand, 0, TRUE) if intRC <> 0 then WScript.Echo "Error returned from time command: " & intRC else WScript.Echo "time command completed successfully" end if strCommand = "cmd.exe /c date 11/01/2004" intRC = objWshShell.Run(strCommand, 0, TRUE) if intRC <> 0 then WScript.Echo "Error returned from date command: " & intRC else WScript.Echo "date command completed successfully" end if WScript.Echo "New time: " & Now
DiscussionIf your server is part of a workgroup or NT 4 domain, you must manually configure the time and date settings on it. You can either set the time and date based on some external time source (such as your watch) or you can configure the server to synch from a time server. The latter is the preferred method because as long as the time server you are pointing your servers to has the correct time, your servers will have the correct time. If your server is part of an Active Directory domain, it will automatically sync its time from a domain controller. Time and date synchronization is handled by the Windows Time service (W32Time), which was originally developed for Windows 2000 and is a compliant implementation of RFC 1769Simple Network Time Protocol (SNTP). For Windows Server 2003, W32Time was updated to support NTP, a more accurate protocol than SNTP. W32Time is highly configurable via the registry. See MS KB 223184 for the list of registry settings you can configure for Windows 2000. A similar KB article has not been produced yet showing the differences for Windows Server 2003. For a good whitepaper on the Windows 2000 Time service, visit the following URL: http://www.microsoft.com/windows2000/docs/wintimeserv.doc. For more on the Windows Server 2003 Time service, visit the following URL: http://www.microsoft.com/Resources/Documentation/windowsserv/2003/all/techref/en-us/W2K3TR_times_intro.asp. See AlsoRFC 1305, RFC 1769, MS KB 216734 (How to Configure an Authoritative Time Server in Windows 2000), MS KB 223184 (Registry Entries for the W32Time Service), MS KB 247873 (Description of Date and Time Format Standards), MS KB 258059 (How to Synchronize the Time on a Windows 2000-Based Computer in a Windows NT 4.0 Domain), MS KB 262680 (A List of the Simple Network Time Protocol Time Servers That Are Available on the Internet), and MS KB 816043 (HOW TO: Turn On Debug Logging in the Windows Time Service) |