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 安全性

对直接调用方的完全信任。 此成员不能由部分信任的代码使用。 有关详细信息,请参阅 使用部分受信任的代码中的库

适用于