James Radford
James Radford

Reputation: 423

Formatting a DateTime to a shorthand month in English

I have a bit of code that's formatting the shorthand month name, see below, but I need the value to always return in English. This code currently seems to be translated into the appropriate language somehow?

Any ideas? Many thanks!

Response.write(myDateTimeValue.ToString("MMM"));  // Needs to always return Jan for all languages

Upvotes: 14

Views: 23293

Answers (5)

Jackson Pope
Jackson Pope

Reputation: 14640

You need to call ToString passing in a IFormatProvider that is for the English culture:

Response.write(month.ToString("MMM", CultureInfo.CreateSpecificCulture("en-GB")));

Upvotes: 2

Sven
Sven

Reputation: 22673

month.ToString("MMM", CultureInfo.InvariantCulture);

InvariantCulture is explicitly for situations where you always need the result to be the same, and always matches en-US. There is no need to create a new instance of CultureInfo.

Upvotes: 13

Jamie Dixon
Jamie Dixon

Reputation: 53991

You can do this by passing a culture info object to the ToString() method as follows:

 CultureInfo ci = new CultureInfo("en-GB");
 Response.write(month.ToString("MMM", ci));

Upvotes: 9

Bala R
Bala R

Reputation: 108937

month.ToString("MMM", new CultureInfo("en-US"))

Upvotes: 2

heisenberg
heisenberg

Reputation: 9759

month.ToString("MMM", CultureInfo.CreateSpecificCulture("en-US"));

Upvotes: 2

Related Questions