共用方式為


ManagementDateTimeConverter.ToDmtfDateTime(DateTime) 方法

定義

將指定的 DateTime 轉換為 DMTF 日期時間格式。

public:
 static System::String ^ ToDmtfDateTime(DateTime date);
public static string ToDmtfDateTime (DateTime date);
static member ToDmtfDateTime : DateTime -> string
Public Shared Function ToDmtfDateTime (date As DateTime) As String

參數

date
DateTime

DateTime,表示要轉換為 DMTF 日期時間的日期時間。

傳回

字串,表示指定 DateTime 的 DMTF 日期時間。

範例

下列範例會將指定的 DateTime 轉換為 DMTF 日期時間格式。

using System;
using System.Management;

// The sample below demonstrates the various conversions
// that can be done using ManagementDateTimeConverter class
class Sample_ManagementDateTimeConverterClass
{
    public static int Main(string[] args)
    {
        string dmtfDate = "20020408141835.999999-420";
        string dmtfTimeInterval = "00000010122532:123456:000";

        // Converting DMTF datetime to System.DateTime
        DateTime dt =
            ManagementDateTimeConverter.ToDateTime(dmtfDate);

        // Converting System.DateTime to DMTF datetime
        string dmtfDateTime =
            ManagementDateTimeConverter.ToDmtfDateTime(DateTime.Now);

        // Converting DMTF time interval to System.TimeSpan
        System.TimeSpan tsRet =
            ManagementDateTimeConverter.ToTimeSpan(dmtfTimeInterval);

        //Converting System.TimeSpan to DMTF time interval format
        System.TimeSpan ts =
            new System.TimeSpan(10,12,25,32,456);
        string dmtfTimeInt  =
            ManagementDateTimeConverter.ToDmtfTimeInterval(ts);

        return 0;
    }
}
Imports System.Management

'The sample below demonstrates the various conversions
' that can be done using ManagementDateTimeConverter class    
Class Sample_ManagementClass
    Public Overloads Shared Function Main(ByVal args() As String) _
        As Integer
        Dim dmtfDate As String = "20020408141835.999999-420"
        Dim dmtfTimeInterval As String = "00000010122532:123456:000"

        'Converting DMTF datetime and intervals to System.DateTime
        Dim dt As DateTime = _
            ManagementDateTimeConverter.ToDateTime(dmtfDate)

        'Converting System.DateTime to DMTF datetime
        dmtfDate = _
            ManagementDateTimeConverter.ToDmtfDateTime(DateTime.Now)

        ' Converting DMTF timeinterval to System.TimeSpan
        Dim tsRet As System.TimeSpan = _
            ManagementDateTimeConverter.ToTimeSpan(dmtfTimeInterval)

        'Converting System.TimeSpan to DMTF time interval format
        Dim ts As System.TimeSpan = _
            New System.TimeSpan(10, 12, 25, 32, 456)
        Dim dmtfTimeInt As String
        dmtfTimeInt = _
            ManagementDateTimeConverter.ToDmtfTimeInterval(ts)

        Return 0
    End Function
End Class

備註

產生的 DMTF 日期時間字串是以目前時區的 UTC 位移為基礎。 DMTF 中最低的有效位數是微秒;在 中 DateTime,最低有效位數為 Ticks,相當於100奈秒。 在轉換期間, Ticks 會轉換成微秒,並將四捨五入為最接近的微秒。

.NET Framework 安全性

完全信任立即呼叫者。 這個成員無法供部分信任的程式碼使用。 如需詳細資訊,請參閱 使用部分信任程式代碼的連結庫

適用於