How Can I Determine the System Time on a Computer?

Hey, Scripting Guy! Question

Hey, Scripting Guy! Is there any way to retrieve the system time as configured on a remote computer?

— JJ

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JJ. There are at least two ways to do this, both using WMI. If you’re running Windows XP or Windows Server 2003, you can use the WMI class Win32_LocalTime. As you can see, that makes it pretty easy to get the system time: you simply connect to the remote computer, query the Win32_LocalTime class, and then echo the desired time values. In other words:

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colItems = objWMIService.ExecQuery(“Select * From Win32_LocalTime”)

For Each objItem in colItems
Wscript.Echo “Month: ” & objItem.Month
Wscript.Echo “Day: ” & objItem.Day
Wscript.Echo “Year: ” & objItem.Year
Wscript.Echo “Hour: ” & objItem.Hour
Wscript.Echo “Minute: ” & objItem.Minute
Wscript.Echo “Second: ” & objItem.Second
Next


If you want to get fancy, you can combine all these separate values into a standard date-time string, something that looks like this: 1/26/2005 8:47:04 AM. Here’s a script that can do just that:

On Error Resume Next

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colItems = objWMIService.ExecQuery(“Select * from Win32_LocalTime”)

For Each objItem in colItems
intMonth = objItem.Month
intDay = objItem.Day
intYear = objItem.Year

dtmDate = intMonth & “/” & intDay & “/” & intYear

intHour = objItem.Hour
If intHour < 12 Then
strAMPM = “AM”
Else
intHour = intHour – 12
strAMPM = “PM”
End If

intMinutes = objItem.Minute
If intMinutes < 10 Then
intMinutes = “0” & intMinutes
End If

intSeconds = objItem.Second
If intSeconds < 10 Then
intSeconds = “0” & intSeconds
End If

dtmTime = intHour & “:” & intMinutes & “:” & intSeconds & ” ” & strAMPM

Wscript.Echo dtmDate & ” ” & dtmTime
Next


We won’t explain each line of code in the script, but for the most part all we’re doing is combining values. In fact, we only do two things even slightly tricky. First we check the hour to see if it’s less than 12; if it is, we add AM to the end of the string; if it’s not, we add a PM to the end of the string. And, yes, this means that the hour is returned in 24-hour format; thus 3:00 in the afternoon comes back as 15:00. That’s why, when checking the hour, we also subtract 12 if the hour is greater than 12. That way, 15:00 gets converted to 3:00 PM.


For both the minutes and seconds, we check to see if the returned value is less than 10. Why? Well suppose we have 8 hours, 6 minutes, and 5 seconds. If we combine these values as-is, our time will be 8:6:5. That doesn’t really look like a time, does it? Therefore, if the minutes or the seconds are less than 10, we add a leading zero. When we do that, we’re now combining 8, 06, and 05, and we get a time that looks like this: 08:06:05. Much better.


Of course, that’s all well and good as long as you’re using Windows XP or Windows Server 2003. But what about people using Windows 2000? Well, that’s OK; you can still get this same information, just not in as straightforward a fashion.


As we noted earlier, the Win32_LocalTime class doesn’t exist on Windows 2000. However, you can still get the system time by querying the Win32_OperatingSystem class and checking the value of the LocalDateTime property. The only problem with this approach is that the date and time come back in UTC (Universal Time Coordinate) format. Consequently, you get a date and time that looks like this: 20050127080605.000000+480. Yuck.


We won’t go into the details surrounding the UTC format; if you’re interested you might want to check out this section of the Microsoft Windows 2000 Scripting Guide. However, we will show you a script that grabs the LocalDateTime value and converts it to a slightly-more readable format:

strComputer = “.”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colItems = objWMIService.ExecQuery(“Select * from Win32_OperatingSystem”)

For Each objItem in colItems
dtmLocalTime = objItem.LocalDateTime
dtmMonth = Mid(dtmLocalTime, 5, 2)
dtmDay = Mid(dtmLocalTime, 7, 2)
dtmYear = Left(dtmLocalTime, 4)
dtmHour = Mid(dtmLocalTime, 9, 2)
dtmMinutes = Mid(dtmLocalTime, 11, 2)
dtmSeconds = Mid(dtmLocalTime, 13, 2)
Next

dtmNewDate = dtmMonth & “/” & dtmDay & “/” & dtmYear
dtmNewTime = dtmHour & “:” & dtmMinutes & “:” & dtmSeconds

Wscript.Echo dtmNewDate & ” ” & dtmNewTime


We haven’t done much in the way of formatting here; for example, we don’t convert 15:00 to 3:00, nor do we indicate whether this is AM or PM. But you can do that yourself, using the same approach we took with the Win32_LocalTime.