I have a DateTime object and I want to output the hour, minute, second, and fractional second as a string that is localized for the current culture.
There are two issues with this.
First issue is there is no standard DateTime
format that will show the fractional seconds. I essentially want to know how to get the long time DateTime format but with fractional seconds.
I could of course get DateTimeFormatInfo.LongTimePattern
and append ".fff" to it and pass it to the DateTime.ToString()
, but some of the culture specific formats, US specifically, end with AM/PM. So it isn't that simple.
The second issue is that DateTime.ToString()
does not appear to localize the number decimal separator. If I decided to just force each culture to use a hard coded custom time format it still will not create a localized string as the number decimal separator will not be culture specific.
To complicate matters further, some cultures have date time formats that use periods as part of their formatting. This makes it difficult to put a placeholder, for example the period and replace it with a culture specific decimal separator.
For now I have resorted to this workaround:
string format = string.Format("HH:mm:ss{0}fff",
CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
string time = DateTime.Now.ToString(format);
Which I think should work for every culture that doesn't have the same decimal separator as the time separator, but that is an assumption.
Of Note: While it would be nice to have a solution to both issues, for my specific application I am more interested in localizing a custom date time format with fractional seconds than using a standard date time format.
ss
, to overcome the AM/PM vs HH problem. – Henk Holterman