DateTimeOffset.Hour 属性
定义
获取由当前 DateTimeOffset 对象所表示的时间的小时组成部分。Gets the hour component of the time represented by the current DateTimeOffset object.
public:
property int Hour { int get(); };
public int Hour { get; }
member this.Hour : int
Public ReadOnly Property Hour As Integer
属性值
当前 DateTimeOffset 对象的小时组成部分。The hour component of the current DateTimeOffset object. 此属性使用 24 小时制;值介于 0 到 23 之间。This property uses a 24-hour clock; the value ranges from 0 to 23.
示例
下面的示例 DateTimeOffset 以三种不同的方式显示对象的小时部分:The following example displays the hour component of a DateTimeOffset object in three different ways:
通过检索属性的值 Hour 。By retrieving the value of the Hour property.
通过 ToString(String) 使用 "H" 格式说明符调用方法。By calling the ToString(String) method with the "H" format specifier.
通过 ToString(String) 使用 "HH" 格式说明符调用方法。By calling the ToString(String) method with the "HH" format specifier.
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.
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 。The Hour property is not affected by the value of the Offset property.
还可以 DateTimeOffset 通过 ToString 使用 "H" 或 "HH" 自定义格式说明符调用方法,创建对象的小时部分的字符串表示形式。You can also create a string representation of a DateTimeOffset object's hour component by calling the ToString method with the "H", or "HH" custom format specifiers.