TimeSpan.ToString("hh:mm") error

DateTime.Now.TimeOfDay.ToString(@"hh\:mm\:ss")

Documentation


According to MSDN TimeOfDay is a TimeSpan. And in the examples of TimeSpan.ToString you see that the : needs to be escaped.

hh\:mm\:ss: 03:00:00

This is also explained on Microsoft's page Custom TimeSpan Format Strings

The custom TimeSpan format specifiers do not include placeholder separator symbols, such as the symbols that separate days from hours, hours from minutes, or seconds from fractional seconds. Instead, these symbols must be included in the custom format string as string literals. For example, "dd\.hh\:mm" defines a period (.) as the separator between days and hours, and a colon (:) as the separator between hours and minutes.

So try:

DateTime.Now.TimeOfDay.ToString("hh\\:mm");      

Tags:

C#

Timespan