fearofawhackplanet
fearofawhackplanet

Reputation: 53446

Datetime string formatting and CultureInfo

I was suprised to see FxCop complaining that I wasn't specifying a CultureInfo in the following:

string s = day.ToString("ddd");

Surely the format string param in ToString is completely independent of CultureInfo? Is there any way which CultureInfo could affect the returned string?

EDIT: sorry, this is a total fail of a question. It's obvious that "ddd" should be dependent on culture settings. I was really thinking more of a case like DateTime.Now.ToString("dd-MM-yy"), but it looks like FxCop doesn't in fact complain in that instance.

Upvotes: 1

Views: 8720

Answers (2)

IsmailS
IsmailS

Reputation: 10863

DateTime.ParseExact(day.ToString(), "ddd", DateTimeFormatInfo.InvariantInfo)

DateTimeFormatInfo.InvariantInfo gives you default culture independent DateTimeFormatInfo.

Upvotes: 0

Espo
Espo

Reputation: 41939

There is an article on MSDN on How to extract the Day of the Week from a Specific Date. You will find many different examples on culture on that page, including this one:

DateTime dateValue = new DateTime(2008, 6, 11);
Console.WriteLine(dateValue.ToString("ddd"));    // Displays Wed

DateTime dateValue = new DateTime(2008, 6, 11);
Console.WriteLine(dateValue.ToString("ddd", 
                  new CultureInfo("fr-FR")));    // Displays mer.

Upvotes: 4

Related Questions