SWbemDateTime 物件

SWbemDateTime物件是用來剖析和設定 Common Information Model (CIM) datetime值的協助程式物件。 它扮演類似 SWbemObjectPath的角色,可提供格式化和解譯物件路徑的協助。 您可以使用 VBScript CreateObject 呼叫來建立 SWbemDateTime 物件。

您可以使用 物件上的方法,從 VT_DATEFILETIME值初始化SWbemDateTime物件並格式化。 使用 物件的屬性,值可以剖析為元件年、月、日、小時、分鐘、秒或微秒。 SWbemDateTime物件可以格式化為 local 或 Coordinated Universal Time (UTC) 值。 如需詳細資訊,請參閱 日期和時間格式

SWbemDateTime 是唯一的 Windows Management Instrumentation (WMI) 腳本物件,這些物件標示為安全,可用於在 Internet Explorer 的 HTML 頁面上執行的初始化和腳本。

成員

SWbemDateTime物件具有下列類型的成員:

方法

SWbemDateTime物件具有這些方法。

方法 描述
GetFileTime 以 BSTR表示的FILETIME日期和時間轉換為 WMI DATETIME格式。
GetVarDate 將 WMI DATETIME 格式化日期和時間值轉換為 VT_DATE
SetFileTime 將 WMI DATETIME 格式轉換為 FILETIME 日期和時間,以 BSTR表示。
SetVarDate VT_DATE 格式化日期和時間轉換為 WMI DATETIME

屬性

SWbemDateTime物件具有這些屬性。

屬性 存取類型 描述

讀取/寫入
CIM 日期時間值的 day 元件。
DaySpecified
讀取/寫入
指出日期是指定為萬用字元,還是保留為萬用字元。
小時
讀取/寫入
CIM 日期時間 值的日元件時數。
HoursSpecified
讀取/寫入
指出小時是指定為萬用字元,還是保留為萬用字元。
IsInterval
讀取/寫入
表示 CIM 日期時間 至少有一個元件代表間隔,而不是日期。
微秒
讀取/寫入
CIM 日期時間 值的微秒元件。
MicrosecondsSpecified
讀取/寫入
指出是否指定或保留為萬用字元的微秒元件。
分鐘
讀取/寫入
CIM 日期時間 值的分鐘元件。
MinutesSpecified
讀取/寫入
指出分鐘元件是指定為萬用字元還是保留為萬用字元。

讀取/寫入
CIM 日期時間 值的月份元件。
MonthSpecified
讀取/寫入
指出月份是指定為萬用字元還是保留為萬用字元。

讀取/寫入
CIM 日期時間 值的秒陣列件。
SecondsSpecified
讀取/寫入
指出秒元件是指定為萬用字元,還是保留為萬用字元。
UTC
讀取/寫入
CIM 日期時間 值的 UTC 元件。
UTCSpecified
讀取/寫入
指出 UTC 元件是指定還是保留為萬用字元。

讀取/寫入
完整的 CIM 日期時間 值。

讀取/寫入
CIM 日期時間 值的年份元件。
YearSpecified
讀取/寫入
指出年份是否指定為萬用字元,或保留為萬用字元。

備註

WMI 會以通用時間座標 (UTC) 格式記錄時間戳記。 UTC 不是大部分開發人員和 IT 系統管理員所使用的格式。 因此,常見的問題是決定如何將 UTC 轉譯成更容易閱讀的內容。 如需如何使用 UTC 的詳細資訊,請參閱 WMI 工作:日期和時間 和使用 WMI 的日期和時間。 您也可以閱讀 [關於時間] (Oh 和 [關於日期]、[關於日期]) 部落格文章,以取得其他資訊。

如果 IsInterval 屬性設定為 FALSE,任何數值欄位都可以有萬用字元值。 具有萬用字元值的欄位包含整個欄位中的星號。

例如 Day的每個屬性都有對應的指定布林值,例如 DaySpecified。 當 DaySpecifiedFALSE時,該值會解譯為間隔,而不是介於 01 到 31 之間的數位。 如果在 CIM 日期時間 值中的任何位置使用間隔, 則 IsInterval 也會設定為 TRUE。 預設值是 CIM 日期時間值,以包含日期,而不是一或多個間隔。

例如,如果SWbemDateTime.DaySpecifiedTRUE則 SWbemDateTime.Value 會包含 SWbemDateTime.Day的目前值,否則為萬用字元值。 在任一情況下 ,IsInterval 屬性都是 FALSE

範例

下列腳本程式碼範例示範如何使用SWbemDateTime物件來剖析從 WMI 存放庫讀取的 datetime 屬性值,Win32_OperatingSystem中的InstallDate屬性。

' 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

下列範例示範如何建立 SWbemDateTime 物件、將日期值儲存在 物件中、將日期顯示為 local 和 Coordinated Universal Time (UTC) ,並將值儲存在新建立的類別和屬性中。 如需 常數 wbemCimtypeDatetime的詳細資訊,請參閱 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_

下列腳本程式碼範例示範如何使用 SWbemDateTime 物件修改從 WMI 存放庫讀取之屬性的間隔值。

' 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_

下列腳本程式碼範例示範如何使用 SWbemDate 物件來讀取 FILETIME 值。

' 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

下列 PowerShell 程式碼會建立 SWbemDateTime 物件的實例、擷取 OS 安裝日期,並將日期轉換為不同的格式

# 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() 

下列 Powershell 程式碼會將程式碼轉譯成可供 CIM 提供者取用的格式。

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

規格需求

需求
最低支援的用戶端
Windows Vista
最低支援的伺服器
Windows Server 2008
標頭
Wbemdisp.h
類型程式庫
Wbemdisp.tlb
DLL
Wbemdisp.dll
CLSID
CLSID_SWbemDateTime
IID
IID_ISWbemDateTime

另請參閱

WbemCimtypeEnum

Datetime

編寫 API 物件的腳本