When outputting a DateTime object to the console, the date and time are displayed.

 DateTime dt = DateTime.Now; Console.WriteLine(dt); 
 10/19/2016 7:25:40 PM 

How can you make so that only time is displayed?

  • 3
    you here - Alex.B
  • @ Alex.B Please try to publish detailed answers containing a specific example of the minimum solution, supplementing them with a link to the source. Answers –references (as well as comments) do not add knowledge to the Runet. - Nicolas Chabanovsky

3 answers 3

There are two ways. Using formats , in a string:

 DateTime dt = DateTime.Now; string time_str = dt.ToString("HH:mm"); 

Using the TimeOfDay property in TimeSpan :

 DateTime dt = DateTime.Now; TimeSpan span = dt.TimeOfDay; 

    To get the current time as a string, use

     DateTime.Now.ToString("T") 

    I hope you will be able to display the string on the screen yourself.

      Here is another option:

       DateTime.Now.ToShortTimeString() 
      • one
        ... or ToLongTimeString() . - VladD