Changing Date format to en-us while culture is fr-ca

后端 未结 6 602
[愿得一人]
[愿得一人] 2021-01-11 17:21

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

6条回答
  •  不知归路
    2021-01-11 18:05

    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.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题