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.