Export (0) Print
Expand All

SWbemDateTime object

The SWbemDateTime object is a helper object to parse and set Common Information Model (CIM) datetime values. It plays a role similar to SWbemObjectPath, which provides assistance to format and interpret object paths. You can use the VBScript CreateObject call to create the SWbemDateTime object.

An SWbemDateTime object can be initialized from and formatted in either VT_DATE or FILETIME values using methods on the object. Using properties of the object, the value can be parsed into component year, month, day, hour, minutes, seconds, or microseconds. The SWbemDateTime object can be formatted into either local or Coordinated Universal Time (UTC) values. For more information, see Date and Time Format.

SWbemDateTime is the only Windows Management Instrumentation (WMI) scripting object that is marked safe for initialization and scripts running on HTML pages in Internet Explorer.

Members

The SWbemDateTime object has these types of members:

Methods

The SWbemDateTime object has these methods.

MethodDescription
GetFileTime

Converts a FILETIME date and time, expressed as a BSTR, to a WMI DATETIME format.

GetVarDate

Converts a WMI DATETIME formatted date and time value to a VT_DATE.

SetFileTime

Converts a WMI DATETIME format to a FILETIME date and time, expressed as a BSTR.

SetVarDate

Converts a VT_DATE formatted date and time to a WMI DATETIME.

 

Properties

The SWbemDateTime object has these properties.

PropertyAccess typeDescription

Day

Read/write

The day component of a CIM datetime value.

DaySpecified

Read/write

Indicates whether the day is specified or left as a wildcard.

Hours

Read/write

The hours of the day component of a CIM datetime value.

HoursSpecified

Read/write

Indicates whether the hour is specified or left as a wildcard.

IsInterval

Read/write

Indicates that at least one component of the CIM datetime represents an interval rather than a date.

Microseconds

Read/write

The microseconds component of a CIM datetime value.

MicrosecondsSpecified

Read/write

Indicates whether the microseconds component is specified or left as a wildcard.

Minutes

Read/write

The minutes component of a CIM datetime value.

MinutesSpecified

Read/write

Indicates whether the minutes component is specified or left as a wildcard.

Month

Read/write

The month component of a CIM datetime value.

MonthSpecified

Read/write

Indicates whether the month is specified or left as a wildcard.

Seconds

Read/write

The seconds component of a CIM datetime value.

SecondsSpecified

Read/write

Indicates whether the seconds component is specified or left as a wildcard.

UTC

Read/write

The UTC component of a CIM datetime value.

UTCSpecified

Read/write

Indicates whether the UTC component is specified or left as a wildcard.

Value

Read/write

The full CIM datetime value.

Year

Read/write

The year component of a CIM datetime value.

YearSpecified

Read/write

Indicates whether or not the year is specified or left as a wildcard.

 

Remarks

WMI records timestamps in universal time coordinate (UTC) format. UTC is not the format that most developers and IT administrators use. Therefore, a common issue is determining how to translate UTC into something more readable. For more information on how to work with UTC, see WMI Tasks: Dates and Times and Working with Dates and Times using WMI . You can also read the It’s About Time (Oh, and About Dates, Too) and How Can I Subtract a Specified Number of Days from a UTC Value? blog posts for additional information.

Any numeric field can have a wildcard value if the IsInterval property is set to FALSE. Fields with wildcard values contain asterisks in the entire field.

Each property, for example Day, has a corresponding specified Boolean value, such as DaySpecified. When DaySpecified is FALSE, then the value is interpreted as an interval rather than a number between 01 and 31. If an interval is used anywhere in the CIM datetime value, then IsInterval is also set to TRUE. The default is for a CIM datetime value to contain a date rather than one or more intervals.

For example, if SWbemDateTime.DaySpecified is TRUE, then SWbemDateTime.Value includes the current value of SWbemDateTime.Day, otherwise it is a wildcard value. The IsInterval property is FALSE in either case.

Examples

The following script code example shows how to use an SWbemDateTime object to parse a datetime property value read from the WMI repository, the InstallDate property in Win32_OperatingSystem.


' Create a new datetime object.
Set dateTime = CreateObject("WbemScripting.SWbemDateTime")

' Retrieve a WMI object that contains a datetime value.
for each os in GetObject( _
    "winmgmts:").InstancesOf ("Win32_OperatingSystem")

    ' The InstallDate property is a CIM_DATETIME. 
    MsgBox os.InstallDate
    dateTime.Value = os.InstallDate

    ' Display the year of installation.
    MsgBox "This OS was installed in the year " & dateTime.Year

    ' Display the installation date using the VT_DATE format.
    MsgBox "Full installation date (VT_DATE format) is " _
    & dateTime.GetVarDate

    ' Display the installation date using the FILETIME format.
    MsgBox "Full installation date (FILETIME format) is " _
    & dateTime.GetFileTime 
next
Set datetime = Nothing

The following example shows how to create an SWbemDateTime object, store a date value in the object, display the date as local and Coordinated Universal Time (UTC), and store the value in a newly created class and property. For more information about the constant wbemCimtypeDatetime, see WbemCimtypeEnum.


' Create an SWbemDateTime object.
Set dateTime = CreateObject("WbemScripting.SWbemDateTime")
' Set the value 
Const wbemCimTypeDatetime = 101

' Construct a datetime value using the intrinsic VBScript CDate
' function interpreting this as a local date/time in
' the Pacific time zone (-8 hrs GMT). Convert to CIM datetime
' using SetVarDate method. The year defaults to current year.
dateTime.SetVarDate (CDate ("January 20 11:56:32"))

' The value in dateTime displays as 
' 20000120195632.000000-480. This is the equivalent time
' in GMT with the specified offset for PST of -8 hrs.
MsgBox "CIM datetime " & dateTime

' The value now displays as B=0/2000 11:56:32 AM because the
' parameter contains the default TRUE value causing the value to be
' interpreted as a local time.
MsgBox "Local datetime " & dateTime.GetVarDate ()

' The value now displays as B=0/2000 7:56:32 PM because the
' parameter value is FALSE, which indicates a GMT time.
' non-local time.
MsgBox "Datetime in GMT " & dateTime.GetVarDate (false)

' Create a new class and add a DateTime property value.
' SWbemServices.Get returns an empty SWbemObject
' which can become a new class. SWbemObject.Path_ returns an
' SWbemObjectPath object. 
set NewObject = GetObject("winmgmts:root\default").Get
NewObject.Path_.Class = "NewClass"

' Add a new property named "InterestingDate" to the NewObject class
' and define its datatype as a CIM datetime value.
NewObject.Properties_.Add "InterestingDate", wbemCimtypeDatetime

' Set the new value of the SWbemDateTime object in the InterestingDate
' property.
NewObject.InterestingDate = dateTime.Value
MsgBox "Datetime in new object " & NewObject.InterestingDate

' Write the new class (named "NewClass") containing
' the SWbemDateTime object to the repository.
NewObject.Put_
WScript.Echo "NewClass is now in WMI repository"

' Clean up the example by deleting the new class from the repository
NewObject.Delete_


The following script code example shows how to use an SWbemDateTime object to modify an interval value on a property that is read from the WMI repository.


' Construct an interval value of 100 days, 1 hour, and 3 seconds.
dateTime.IsInterval = true 
dateTime.Day = 100
dateTime.Hours = 1
dateTime.Seconds = 3

' The datetime displays as 00000100010003.000000:000.
MsgBox "Constructed interval value " & datetime

' Retrieve an empty WMI object and add a datetime property. 
Const wbemCimTypeDatetime = 101
Set NewObject = GetObject("winmgmts:root\default").Get
NewObject.Path_.Class = "Empty"
NewObject.Properties_.Add "InterestingDate", wbemCimtypeDatetime

' Set the new value in the property and update. 
NewObject.InterestingDate = dateTime.Value
MsgBox "NewObject.InterestingDate = " & NewObject.InterestingDate

' Write the new SWbemDateTime object to the repository.
NewObject.Put_

' Delete the object.
NewObject.Delete_

The following script code example shows how to use an SWbemDate object to read a FILETIME value.


' Create a new datetime object.
Set datetime = CreateObject("WbemScripting.SWbemDateTime")

' Set from a FILETIME value (non-local).
' Assume a timezone -7 hrs. GMT.
MsgBox "FILETIME value " & "126036951652030000"
datetime.SetFileTime "126036951652030000", false

' Displays as 5/24/2000 7:26:05 PM.
MsgBox "GMT time " & dateTime.GetVarDate   

' Set from a FILETIME value (local).
datetime.SetFileTime "126036951652030000"

' Displays as 5/25/2000 2:26:05 AM.
MsgBox "Same value in local time " & dateTime.GetVarDate
Set datetime = Nothing

The following PowerShell code creates an instance of a SWbemDateTime object, retrieves the OS install date, and converts the date to a different format


# Create swbemdatetime object
$datetime = New-Object -ComObject WbemScripting.SWbemDateTime

#  Get OS installation time and assign to datetime object
$os = Get-WmiObject -Class Win32_OperatingSystem
$dateTime.Value = $os.InstallDate

# Now display the time
"This OS was installed in the year {0}"           -f $dateTime.Year
"Full installation date (VT_DATE format) is {0}"  -f $dateTime.GetVarDate()
"Full installation date (FILETIME format) is {0}" -f $dateTime.GetFileTime() 


The following Powershell code translates the code into a format ready to be consumed by a CIM provider.


 $time = (Get-Date)
 $objScriptTime = New-Object -ComObject WbemScripting.SWbemDateTime
 $objScriptTime.SetVarDate($time)
 $cimTime = $objScriptTime.Value

Requirements

Minimum supported client

Windows Vista

Minimum supported server

Windows Server 2003

Type library

Wbemdisp.tlb

DLL

Wbemdisp.dll

See also

WbemCimtypeEnum
DATETIME
Scripting API Objects

 

 

Show:
© 2014 Microsoft