DateTimeOffset.Hour 屬性

定義

取得目前 DateTimeOffset 物件表示之時間的時數元件。

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

屬性值

Int32

目前 DateTimeOffset 物件的時數元件。 這個屬性使用 24 小時制,且值的範圍從 0 到 23。

範例

下列範例會以三種不同的方式顯示 物件的小時元件 DateTimeOffset

DateTimeOffset theTime = new DateTimeOffset(2008, 3, 1, 14, 15, 00,
                                       DateTimeOffset.Now.Offset);
Console.WriteLine("The hour component of {0} is {1}.",
                  theTime, theTime.Hour);

Console.WriteLine("The hour component of {0} is{1}.",
                  theTime, theTime.ToString(" H"));

Console.WriteLine("The hour component of {0} is {1}.",
                  theTime, theTime.ToString("HH"));
// The example produces the following output:
//    The hour component of 3/1/2008 2:15:00 PM -08:00 is 14.
//    The hour component of 3/1/2008 2:15:00 PM -08:00 is 14.
//    The hour component of 3/1/2008 2:15:00 PM -08:00 is 14.
let theTime = DateTimeOffset(2008, 3, 1, 14, 15, 00, DateTimeOffset.Now.Offset)
printfn $"The hour component of {theTime} is {theTime.Hour}."

printfn $"""The hour component of {theTime} is{theTime.ToString " H"}."""

printfn $"The hour component of {theTime} is {theTime:HH}."

// The example produces the following output:
//    The hour component of 3/1/2008 2:15:00 PM -08:00 is 14.
//    The hour component of 3/1/2008 2:15:00 PM -08:00 is 14.
//    The hour component of 3/1/2008 2:15:00 PM -08:00 is 14.
Dim theTime As New DateTimeOffset(#3/1/2008 2:15PM#, _
                                       DateTimeOffset.Now.Offset)
Console.WriteLine("The hour component of {0} is {1}.", _
                  theTime, theTime.Hour)

Console.WriteLine("The hour component of {0} is{1}.", _
                  theTime, theTime.ToString(" H"))

Console.WriteLine("The hour component of {0} is {1}.", _
                  theTime, theTime.ToString("HH"))
' The example produces the following output:
'    The hour component of 3/1/2008 2:15:00 PM -08:00 is 14.
'    The hour component of 3/1/2008 2:15:00 PM -08:00 is 14.
'    The hour component of 3/1/2008 2:15:00 PM -08:00 is 14.

備註

屬性 Hour 不會受到 屬性的值 Offset 影響。

您也可以使用 「H」 或 「HH」 自訂格式規範呼叫 ToString 方法來建立物件的小時元件的字串表示 DateTimeOffset

適用於