Microsoft .NET Framework method ToString() is culture unsafe




I’ve found out that the output of ToString() method depends on Current Culture and regional settings for that culture. So the following c#.NET code will return different strings according to the current culture.

DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");

The output will be the following

2007-06-25 12:54:15.005

if current culture is “(en-EN)”

The output will be something like

2007-06-25 12.54.15.005

if current culture is “(it-IT)”

To avoid differences and to get exactly the same output as specified in format string you have to use expression

DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff",CultureInfo.InvariantCulture);

2 comments to Microsoft .NET Framework method ToString() is culture unsafe

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>