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?
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?
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() ToLongTimeString() . - VladDSource: https://ru.stackoverflow.com/questions/579175/
All Articles