As stated in documentation, one of the differences between DateTime.ToString
and TimeSpan.ToString
format specifiers is the following: 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.
In contrast with TimeSpan
(see table of format specifiers in docs), DateTime
format specifiers include predefined symbols for Date separator /
, and for Time separator :
. It means that for example for Italian culture semicolon will be recognized as time separator (not the literal) and will be replaced with .
symbol:
// outputs 09.57.18 instead of 09:57:18 because of Italian culture.
Console.WriteLine(DateTime.Now.ToString("hh:mm:ss", CultureInfo.GetCultureInfo("it-IT")));
I think .NET designers made such difference between DateTime
and TimeSpan
string formatters intentionally, and it is quite reasonable. This is because historically Date/Time were formatted differently for different cultures. And .NET tried to provide globalization means for that matter along with DateTime
type. But TimeSpan
did not get such 'globalization' duties, it is just a type representing period of time, and formatting of it is not tied to any culture specifics (if they are ever existed), but instead formatting of it is constant in different culture settings.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…