Get the time of day for DateTime in CSharp

Description

The following code shows how to get the time of day for DateTime.

Example


//  w  ww .j a  v a  2s  . c  o  m
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.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var