0

我需要覆盖当前(或指定)CurrentCulture 对象的默认 DayNames。

我试图通过几种方式来实现这一点:

1) 在 Global.asax 级别:

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e) {
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(XXX);
    string[] newNames = { YYY };
    System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.DayNames = newNames;
}

2)在控制器级别:

public HomeController() {
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(XXX);
    string[] newNames = { YYY };
    System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.DayNames = newNames;
}

正确应用了指定的“CurrentCulture”对象(根据指定的 XXX 文化名称)。@DateTime.Now.ToString("ddd")我可以通过表达式看到特定于文化的日期名称。但是,指定的“DayNames”对象似乎被忽略了(但是,我可以看到“CurrentCulture”和“DayNames”属性都在调试器中正确应用)。

4

1 回答 1

0

有必要改写默认的 AbbreviatedDayNames:

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e) {
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(...);
    System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.AbbreviatedDayNames = ...;
}

@DateTime.Now.ToString("ddd")
于 2012-09-26T20:01:17.583 回答