如何设置我的项目的本地化,以便DateTime.Parse将使用它?

时间:2022-05-24 22:49:21

Our software was deployed to a server that was set in the control panel to be within the US, with MM/dd/yyyy format. As a result, instances of DateTime.TryParse() in the code used the local format of the machine, not the format of our dev machines which are UK dd/MM/yyyy.

我们的软件部署到控制面板中设置的服务器,位于美国境内,采用MM / dd / yyyy格式。因此,代码中的DateTime.TryParse()实例使用了本机的本地格式,而不是我们的dev机器的格式,这些格式是UK dd / MM / yyyy。

I can see that I can force it to use a specific localization, but what I'd like to do is pop something in the global file or web/app config to make it always use UK format, so that I don't have to specify it for every DateTime operation.

我可以看到我可以强制它使用特定的本地化,但我想做的是在全局文件或web / app配置中弹出一些内容,使其始终使用英国格式,这样我就不必为每个DateTime操作指定它。

Any idea how I do this? Bonus if I can configure it in Visual Studio to force this into new projects so people don't forget.

知道我是怎么做到的吗?如果我可以在Visual Studio中配置它以强制它进入新项目,那么人们不会忘记。

2 个解决方案

#1


0  

Try this Use this approach if you want to set the default culture and user interface culture for all the pages in a Web application.

尝试此操作如果要为Web应用程序中的所有页面设置默认区域性和用户界面区域,请使用此方法。

<configuration>
   <system.web>
      <globalization culture="en-GB" uiCulture="Fr-FR"/>
   </system.web>
</configuration>

#2


0  

As far as I know, there is no such setting (for desktop applications). But you could put this into all entry points of your application(s):

据我所知,没有这样的设置(对于桌面应用程序)。但是你可以把它放到你的应用程序的所有入口点:

var culture = new System.Globalization.CultureInfo("en-GB");
System.Threading.Thread.CurrentThread.CurrentCulture = culture;
System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
System.Globalization.CultureInfo.DefaultThreadCurrentCulture = culture;
System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = culture;

However, that would make localization impossible. I would prefer explicitly specifying the culture in the parse methods.

但是,这会使本地化变得不可能。我更希望在解析方法中明确指定文化。

#1


0  

Try this Use this approach if you want to set the default culture and user interface culture for all the pages in a Web application.

尝试此操作如果要为Web应用程序中的所有页面设置默认区域性和用户界面区域,请使用此方法。

<configuration>
   <system.web>
      <globalization culture="en-GB" uiCulture="Fr-FR"/>
   </system.web>
</configuration>

#2


0  

As far as I know, there is no such setting (for desktop applications). But you could put this into all entry points of your application(s):

据我所知,没有这样的设置(对于桌面应用程序)。但是你可以把它放到你的应用程序的所有入口点:

var culture = new System.Globalization.CultureInfo("en-GB");
System.Threading.Thread.CurrentThread.CurrentCulture = culture;
System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
System.Globalization.CultureInfo.DefaultThreadCurrentCulture = culture;
System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = culture;

However, that would make localization impossible. I would prefer explicitly specifying the culture in the parse methods.

但是,这会使本地化变得不可能。我更希望在解析方法中明确指定文化。