Changing Date format to en-us while culture is fr-ca
I'm working on localizing a website in French. However I am not supposed t开发者_如何学JAVAo 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).
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.
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;
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.
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.
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();
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.
精彩评论