DateTimeOffset.Hour Egenskap

Definition

Hämtar timkomponenten för den tid som representeras av det aktuella DateTimeOffset objektet.

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

Egenskapsvärde

Timkomponenten för det aktuella DateTimeOffset objektet. Den här egenskapen använder en 24-timmarsklocka. värdet varierar från 0 till 23.

Exempel

I följande exempel visas timkomponenten för ett DateTimeOffset objekt på tre olika sätt:

  • Genom att hämta värdet för egenskapen Hour.

  • Genom att anropa ToString(String) metoden med formatspecificeraren "H".

  • Genom att anropa ToString(String) metoden med formatspecificeraren "HH".

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.

Kommentarer

Egenskapen Hour påverkas inte av egenskapens Offset värde.

Du kan också skapa en strängrepresentation av ett DateTimeOffset objekts timkomponent genom att anropa ToString metoden med anpassade formatspecificerare för "H" eller "HH".

Gäller för