C# DateTime TimeOfDay
Description
DateTime TimeOfDay
gets the time of day for this instance.
Syntax
DateTime.TimeOfDay
has the following syntax.
public TimeSpan TimeOfDay { get; }
Example
The following example displays the value of the TimeOfDay property for an array of DateTime values.
It contrasts the return value with the string returned by the "t" standard format string in a composite formatting operation.
//ww w . j a v a 2 s . com
using System;
public class Example
{
public static void Main()
{
DateTime[] dates = { DateTime.Now,
new DateTime(2014, 9, 14, 9, 28, 0),
new DateTime(2013, 5, 28, 10, 35, 0),
new DateTime(2000, 12, 25, 14, 30, 0) };
foreach (var date in dates) {
Console.WriteLine("Day: {0:d} Time: {1:g}", date.Date, date.TimeOfDay);
Console.WriteLine("Day: {0:d} Time: {0:t}\n", date);
}
}
}
The code above generates the following result.