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).
In America, the date is formally written in month/day/year form. Thus, “January 1, 2011” is widely considered to be correct. In formal usage, it is not appropriate to omit the year, or to use a purely numerical form of the date.
The international format yyyy-mm-dd or yyyymmdd is also accepted, though this format is not commonly used. The formats d. 'month name' yyyy and in handwriting d/m-yy or d/m yyyy are also acceptable.)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With