C# DateTime Hour

Description

DateTime Hour gets the hour component of the date represented by this instance.

Syntax

DateTime.Hour has the following syntax.


public int Hour { get; }

Example

The following example demonstrates the Hour property.


using System;/*from  w w w  .j a  v a2 s.c om*/
public class MainClass{
  public static void Main(String[] argv){  
    System.DateTime moment = new System.DateTime(
              1999, 1, 13, 3, 57, 32, 11);
  // Hour gets 3. 
  int hour = moment.Hour;

    System.Console.WriteLine(hour);
  }
}

The code above generates the following result.

Example 2

The value of the Hour property is always expressed using a 24-hour clock. To retrieve a string that represents the hour of a date and time using a 12-hour clock, call the DateTime.ToString(String) or DateTime.ToString(String, IFormatProvider) method with the "h" custom format specifier.


using System;//w w w  .  ja  va 2 s.c o  m
public class MainClass{

  public static void Main(String[] argv){  
    DateTime date1 = new DateTime(2014, 4, 1, 18, 53, 0);
    System.Console.WriteLine(date1.ToString("%h"));             
    System.Console.WriteLine(date1.ToString("h tt"));          

  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version