Compartilhar via


Working with Dates and Times using WMI

Microsoft® Windows® 2000 Scripting Guide

One of the more confusing aspects of WMI is the way WMI handles dates and times. For example, consider this simple script, which returns the date that the operating system was installed on a computer:

strComputer = "."
Set objSWbemServices = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOS = objSWbemServices.ExecQuery("SELECT * FROM Win32_OperatingSystem")
For Each objOS in colOS
    Wscript.Echo objOS.InstallDate
Next

When this script is run under CScript, output similar to the following appears in the command window:

20011224113047.000000-480

This is not a misprint; 20011224113047.000000-480 really is the date that the operating system was installed on the computer. In fact, this value indicates that the operating system was installed on December 24, 2001. The date is correct; the problem lies not with the date but with the fact that the date is displayed in the Universal Time Coordinate (UTC) format.

In the UTC format, dates are displayed as yyyymmddHHMMSS.xxxxxx±UUU, where:

  • yyyy represents the year.

  • mm represents the month.

  • dd represents the day.

  • HH represents the hour (in 24-hour format).

  • MM represents the minutes.

  • SS represents the seconds.

  • xxxxxx represents the milliseconds.

  • UUU represents the difference, in minutes, between the local time zone and Greenwich Mean Time (GMT).

Consequently, the value 20011224113047.000000-480 is translated like this:

  • 2001 is the year.

  • 12 is the month (December).

  • 24 is the day.

  • 11 is the hour of the day (in 24-hour format).

  • 30 is the minutes.

  • 47 is the seconds.

  • 000000 is the milliseconds.

  • 480 is the number of minutes different from Greenwich Mean Time.

Needless to say, the UTC format creates a number of problems for both script writers and script users. For one thing, it is difficult to determine dates and times at a glance. For another, it is not exactly a straightforward process to query for items based on date-time values. Suppose you want to retrieve a list of all the folders on a computer that were created after September 3, 2002. This seems like a simple enough task; after all, the Win32_Directory class includes a CreationDate property that specifies the date the folder was created.

Unfortunately, you cannot simply ask WMI to return a list of folders created after 9/3/2002. This script, which attempts to do just that, will run, but will not return any data.

dtmTargetDate = #9/3/2002#
strComputer = "."
Set objSWbemServices = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDirectories = objSWbemServices.ExecQuery _
  ("SELECT * FROM Win32_Directory WHERE CreationDate > '" & dtmTargetDate & "'")
For Each objDirectory In colDirectories
    Wscript.Echo objDirectory.Name
Next

Instead, you have to pass WMI the date using UTC format:

dtmTargetDate = "20020903000000.000000-480"
strComputer = "."
Set objSWbemServices = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDirectories = objSWBemServices.ExecQuery _
  ("SELECT * FROM Win32_Directory WHERE CreationDate > '" & dtmTargetDate & "'")
For Each objDirectory In colDirectories
    Wscript.Echo objDirectory.Name
Next

Because dates are very important in system administration, and because WMI is the technology of choice for most system administration tasks, it is important for script writers to be able to carry out two tasks:

  • Convert WMI dates to a standard date-time format.

  • Convert a standard date to a WMI date-time format.