are.ehibou.com

June 21, 2007

Microsoft .NET Framework method ToString() is culture unsafe

Filed under: Side Notes — admin @ 12:59 pm

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);
  • Share/Bookmark

2 Comments »

  1. what the meaning of overwrite method tostrig in class and how could i use it in my class ?
    thnx

    Comment by aizat — September 15, 2008 @ 10:45 pm

  2. Have no idea what do you meen …

    Comment by aurimas.norkevicius — September 15, 2008 @ 11:04 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress