Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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).

like image 518
Mohammad Nadeem Avatar asked Mar 25 '10 06:03

Mohammad Nadeem


People also ask

How do you format date in USA?

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.

What is the international date format?

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.)


1 Answers

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.

like image 183
bernhof Avatar answered Sep 29 '22 13:09

bernhof