views:

363

answers:

5

I'm working on localizing a website in French. However I am not supposed to change the date format to French. It must remain as per en-us format even if the culture is set to fr-ca i.e, when rest of the contents are in French, the date format should still be in English(en-us).

+2  A: 

On the string, when you display the date, do the following:

CultureInfo ci = new CultureInfo("en-US");
string s = dateTimeObject.ToString(ci);

This is a simplified example however, you just need to do the necessary work you want to do on the DateTime object.

Kyle Rozendo
If I do it this way I'll have to change the code everywhere. Can't this be done globally?
Nadeem
@Nadeem: It's possible to make your own CultureInfo. See my updated answer.
Bernhof
+2  A: 

Whenever you write a date to the page, use the following syntax:

myDate.ToString("d", new CultureInfo("en-US"));

or

string.Format(
  new CultureInfo("en-US"),
  "This is a string containing a date: {0:d}",
  myDate);

The CultureInfo class resides in the System.Globalization namespace and d in the above is the format in which to output the date. See this cheat sheet for more format strings.


To easily change the date format globally you could create a custom CultureInfo, based on an existing CultureInfo (in your case "fr-CA"), modifying only the date formats. I don't have any experience in this, but the linked aricle and this article explains how it's done. Supposedly, it's not too difficult.

I imagine that setting System.Threading.Thread.CurrentThread.CurrentCulture to an instance of your custom CultureInfo (e.g. in the Page.Load event) should do the job.

Bernhof
A: 

Thanks Guys !!!! Seems like your sugessions are working for me. I tried creating a custom culture which extends the features of fr-ca and changes its date format to en-us. Here is the code

CultureInfo ci = new CultureInfo("fr-ca"); DateTimeFormatInfo dateformat = new DateTimeFormatInfo(); dateformat.FullDateTimePattern = "dddd, mmmm dd, yyyy h:mm:ss tt";// Date format of en-us ci.DateTimeFormat = dateformat; CultureAndRegionInfoBuilder obj = new CultureAndRegionInfoBuilder("fr-ca", CultureAndRegionModifiers.Replacement); obj.LoadDataFromCultureInfo(ci); obj.Register();

Once the code registers new fr-ca, the date format of the fr-ca will be same as that of en-us. The code can be used in Page_Load.

Nadeem
+1  A: 

Here is the above code in code snipet :

CultureInfo ci = new CultureInfo("fr-ca"); 

DateTimeFormatInfo dateformat = new DateTimeFormatInfo();



dateformat.FullDateTimePattern = "dddd, mmmm dd, yyyy h:mm:ss tt";// Date format of en-us



ci.DateTimeFormat = dateformat; 

CultureAndRegionInfoBuilder obj = new CultureAndRegionInfoBuilder("fr-ca", CultureAndRegionModifiers.Replacement);


 obj.LoadDataFromCultureInfo(ci); 

obj.Register();
Nadeem
A: 

Surprisingly I got a very simple answer. While setting Culture and UICulture, all I need to do is to set the Culture Property to en-us always. This will result in showing the date format in English always.

Nadeem