Do not use TimeOfDay
. Directly do ToString()
on DateTime.Now
:
DateTime.Now.ToString("hh:mm");
TimeOfDay
is a TimeSpan
. The docs clearly state this about TimeSpan.ToString(string format)
overload:
The format parameter can be any valid standard or custom format specifier for TimeSpan values. If format is equal to String.Empty or is null, the return value of the current TimeSpan object is formatted with the common format specifier ("c"). If format is any other value, the method throws a FormatException.
If you must do it using a TimeSpan
variable, you can simply add it to a DateTime
variable that has its time part set to zero, and then use its ToString()
:
DateTime.Today.Add(YourTimeSpanVariable).ToString("hh:mm");