Home > Articles > Home & Office Computing > Microsoft Windows Desktop

  • Print
  • + Share This
From the author of

WMI Dates and Times

If you ran the script, you might be puzzling over one minor problem: The time and date information extracted from the CreationDate, LastModified, and LastAccessed properties is utterly incomprehensible. Rather than a sanely formatted date, you see a very large number! The reason for this is that the WMI date format is entirely different from the VBScript date format (and, for that matter, from the Windows 2000 internal date format). This large number is in fact nothing more than a long string of the form "yyyymmddHHMMSS.mmmmmmsUUU", where yyyy is the four-digit year; mm and dd are the two-digit month and date, respectively; HHMMSS represents hours, minutes, and seconds; mmmmmm represents microseconds(!); and sUUU represents the offset from GMT (Greenwich Mean Time).

If you are wondering why Microsoft would choose such an alien representation for its WMI dates, the answer is that this platform-independent format is actually part of the Web-Based Enterprise Management (WBEM) specification as defined by the Distributed Management Task Force (DMTF), so Microsoft had no choice here. On the other hand, you might be wondering about the more practical matter of how you can turn this mess into a civilized, readable date. This is actually rather simple. It just requires a little bit of string manipulation and a couple of VBScript conversion functions. Sadly, a detailed discussion of string manipulation in VBScript would take us a little too far from the matter at hand, so I will simply present here a short function that solves the problem without dwelling too much on the details:

Function GetVBDate(wd)
  GetVBDate = DateSerial(left(wd,4),mid(wd,5,2),mid(wd,7,2)) _
       + TimeSerial(mid(wd,9,2),mid(wd,11,2),mid(wd,13,2))
End Function

If you append these lines to the end of your script, this function can be used to convert the WMI dates to VBScript format, thereby giving you a sane output. To use this function, simply replace these lines:

Wscript.echo "The file was created on: " & refFile.CreationDate
Wscript.echo "The file was last modified on: " & refFile.LastModified
Wscript.echo "The file was last accessed on: " & refFile.LastAccessed

with these:

Wscript.echo "The file was created on: " & GetVBDate(refFile.CreationDate)
Wscript.echo "The file was last modified on: " & _
        GetVBDate(refFile.LastModified)
Wscript.echo "The file was last accessed on: " & _
        GetVBDate(refFile.LastAccessed)

This syntax means "Evaluate refFile.LastAccessed, pass it as a parameter to the GetVBDate() function, which is defined farther down in the script, and echo the value that this function returns." Forgetting to paste the function itself into the script and attempting to run this code results in a VBScript runtime error of the form Type mismatch:'GetVBDate'. Or, if you start your script with the Option Explicit directive, you see the equally helpful runtime error Variable Undefined.

  • + Share This
  • 🔖 Save To Your Account