简体   繁体   English

DateTime.ToString(“ g”)fr_CA

[英]DateTime.ToString(“g”) fr_CA

There is an aberration with date formatting using fr_CA culture. 使用fr_CA文化的日期格式存在偏差。

DateTime.ToString("g") returns "YYYY-MM-DD 00:00" format because "YYYY-MM-DD" is the official Canada date format. DateTime.ToString("g")返回“ YYYY-MM-DD 00:00”格式,因为“ YYYY-MM-DD”是加拿大的官方日期格式。 The problem is that this format is not used at all neither by francophones and anglophones !! 问题是法语和英语都根本不使用这种格式!

So my question is: is there a way to bypass the default implementation of DateTime.ToString(string format) so that I can use "dd/mm/yyyy" instead for fr_CA which corresponds to the real usage ? 所以我的问题是:有没有一种方法可以绕过DateTime.ToString(string format)的默认实现,以便我可以使用“ dd / mm / yyyy”代替fr_CA,它对应于实际用法? and the default implementation for all other cultures. 以及所有其他区域性的默认实现。

And is it possible to do it without using another method name ? 是否可以不使用其他方法名称就可以做到这一点?

You can handle this on a case by case basis. 您可以根据情况进行处理。 If the culture is fr-CA , replace it with a modified copy when your app starts: 如果区域性是fr-CA ,则在您的应用启动时将其替换为修改后的副本:

if (Thread.CurrentThread.CurrentCulture.Name == "fr-CA")
{
    var frca = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
    frca.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
    Thread.CurrentThread.CurrentCulture = frca;
}

And do the same for any culture you need to "customize". 并对您需要“自定义”的任何文化进行相同的操作。

Note that new threads will still start with the system-defined culture; 请注意,新线程仍将以系统定义的区域性开始; if you're using .NET 4.5 or later, you can override that behavior using the DefaultThreadCurrentCulture property: 如果使用的是.NET 4.5或更高版本,则可以使用DefaultThreadCurrentCulture属性覆盖该行为:

CultureInfo.DefaultThreadCurrentCulture = frca;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM