DateTime.Millisecond 屬性

定義

取得這個執行個體所表示日期的毫秒元件。

public:
 property int Millisecond { int get(); };
public int Millisecond { get; }
member this.Millisecond : int
Public ReadOnly Property Millisecond As Integer

屬性值

Int32

毫秒元件,以 0 到 999 之間的值表示。

範例

下列範例會示範 Millisecond 屬性。

System::DateTime moment = System::DateTime(
   1999, 1, 13, 3, 57, 32, 11 );

// Year gets 1999.
int year = moment.Year;

// Month gets 1 (January).
int month = moment.Month;

// Day gets 13.
int day = moment.Day;

// Hour gets 3.
int hour = moment.Hour;

// Minute gets 57.
int minute = moment.Minute;

// Second gets 32.
int second = moment.Second;

// Millisecond gets 11.
int millisecond = moment.Millisecond;
System.DateTime moment = new System.DateTime(
                                1999, 1, 13, 3, 57, 32, 11);
// Year gets 1999.
int year = moment.Year;

// Month gets 1 (January).
int month = moment.Month;

// Day gets 13.
int day = moment.Day;

// Hour gets 3.
int hour = moment.Hour;

// Minute gets 57.
int minute = moment.Minute;

// Second gets 32.
int second = moment.Second;

// Millisecond gets 11.
int millisecond = moment.Millisecond;
Dim moment As New System.DateTime(1999, 1, 13, 3, 57, 32, 11)

' Year gets 1999.
Dim year As Integer = moment.Year

' Month gets 1 (January).
Dim month As Integer = moment.Month

' Day gets 13.
Dim day As Integer = moment.Day

' Hour gets 3.
Dim hour As Integer = moment.Hour

' Minute gets 57.
Dim minute As Integer = moment.Minute

' Second gets 32.
Dim second As Integer = moment.Second

' Millisecond gets 11.
Dim millisecond As Integer = moment.Millisecond

備註

您可以 Millisecond 使用 "fff" 格式規範來顯示內容的字串表示。 例如,下列程式碼會顯示一個字串,其中包含在主控台中的日期和時間的毫秒數。

DateTime date1 = new DateTime(2008, 1, 1, 0, 30, 45, 125);
Console.WriteLine("Milliseconds: {0:fff}",
                  date1);           // displays Milliseconds: 125
Dim date1 As Date = New Date(2008, 1, 1, 0, 30, 45, 125)
Console.WriteLine("Milliseconds: {0:fff}", _
                  date1)       ' displays Milliseconds: 125

您也可以使用 "o" 標準格式規範來顯示毫秒元件,以及日期和時間值的其他元件。 例如:

DateTime date2 = new DateTime(2008, 1, 1, 0, 30, 45, 125);
Console.WriteLine("Date: {0:o}",
                  date2);
// Displays the following output to the console:
//      Date: 2008-01-01T00:30:45.1250000
Dim date2 As New Date(2008, 1, 1, 0, 30, 45, 125)
Console.WriteLine("Date: {0:o}", date2)           
' Displays the following output to the console:
'      Date: 2008-01-01T00:30:45.1250000

不過,"o" 格式規範的目的不是要用於顯示,而不是用於來回往返或儲存 DateTime 值。 您也可以使用自訂格式字串,將毫秒與其他日期和時間元件一起顯示,如下列範例所示。

DateTime date3 = new DateTime(2008, 1, 1, 0, 30, 45, 125);
Console.WriteLine("Date with milliseconds: {0:MM/dd/yyy HH:mm:ss.fff}",
                  date3);
// Displays the following output to the console:
//       Date with milliseconds: 01/01/2008 00:30:45.125
Dim date3 As New Date(2008, 1, 1, 0, 30, 45, 125)
Console.WriteLine("Date with milliseconds: {0:MM/dd/yyy HH:mm:ss.fff}", _
                  date3)
' Displays the following output to the console:
'       Date with milliseconds: 01/01/2008 00:30:45.125

適用於