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
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 = "ffffdd, 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.
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.
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.
To change how dates are formatted 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.
Or, use the CultureInfo class to specify culture on a per-string basis:
CultureInfo culture = new CultureInfo("en-US");
Whenever you write a date to the page, use the following syntax:
myDate.ToString("d", culture);
or
string.Format(
culture,
"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 John Sheehan's ".NET Format String Quick Reference" cheat sheet for more on format strings.
Here is the above code in code snipet :
CultureInfo ci = new CultureInfo("fr-ca");
DateTimeFormatInfo dateformat = new DateTimeFormatInfo();
dateformat.FullDateTimePattern = "ffffdd, 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();
In my case I had to set the language of the app, also determine if the language is right-to-left language, but also needed to keep the standard datetime format. So that was what I did:
string culture = "ar-SA";
//Set language and culture to Arabic
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(culture);
//But independent of language, keep datetime format same
DateTimeFormatInfo englishDateTimeFormat = new CultureInfo("en-CA").DateTimeFormat;
Thread.CurrentThread.CurrentCulture.DateTimeFormat = englishDateTimeFormat;