简体   繁体   English

为什么不同计算机或操作系统上的相同文化的日期格式不同?

[英]Why is the date format different for the same culture on different computers or OS?

I have problem with date formate while hosting site which is developed using C# .net MVC . 我在托管使用C#.net MVC开发的网站时遇到日期格式问题。

In my development machine( OS:Windows 7, .net Framework 4.5 ) date formate for Spanish-Panama(es-PA) is mm/dd/yy and in server machine( OS:Windows 8, .net Framework 4.5 ) it is giving d/m/yy format for same culture. 在我的开发机器( OS:Windows 7,.net Framework 4.5 )中,西班牙语 - 巴拿马(es-PA)的日期格式为mm / dd / yy,而在服务器机器( OS:Windows 8,.net Framework 4.5 )中,它给出了相同文化的d / m / yy格式。

I checked it with by using simple console application. 我使用简单的控制台应用程序检查了它。

public static void Main()
{
    DateTime dt = DateTime.Now;
    Thread.CurrentThread.CurrentCulture = new CultureInfo("es-PA");

    Console.WriteLine(dt.ToString("d"));

    Console.ReadLine();
}

Output on development machine is: 10/08/2015 开发机器的输出是:10/08/2015

Output on server machine is : 8/10/15 服务器机器上的输出是:8/10/15

I also checked by changing Language and Regional but in both machine default format is different. 我还通过更改语言和区域进行检查,但两种机器的默认格式都不同。

Why format is different in different machine for same culture? 为什么不同机器的格式在同一文化中有所不同? Is there any solution for this? 这有什么解决方案吗?

Why format is different in different machine for same culture? 为什么不同机器的格式在同一文化中有所不同?

Because formats are updated over time, in different OS releases and in patches/hotfixes. 因为格式会随着时间的推移而在不同的操作系统版本和补丁/修补程序中更新。 .NET is fetching the format from the OS (I believe, anyway), so it's the OS that's the source of the difference - as you've seen in the regional settings. .NET正在从操作系统中获取格式(我相信,无论如何),因此操作系统是差异的来源 - 正如您在区域设置中看到的那样。

Is there any solution for this? 这有什么解决方案吗?

Check that both machines are up to date with Windows Update, and possibly check for any hotfixes available for this. 检查两台计算机是否都是最新的Windows Update,并可能检查可用于此的任何修补程序。 Otherwise, you could always manually update the one you want to change, but obviously that's far from ideal. 否则,您可以随时手动更新要更改的那个,但显然这远非理想。

It's possible that Windows 8 just has more up-to-date information than Windows 7 in a way that isn't going to be addressed by Windows update :( Windows 8可能只有Windows 7提供的更新信息,其方式不会被Windows更新解决:(

Cultures have multiple accepted formats. 文化有多种可接受的格式。 The thread gets it culture (and thus format) from the account the thread is executing as. 线程从线程执行的帐户获取文化(从而格式化)。

For example if you are running IIS express on your dev machine (IIS express uses the windows users account for ASP.NET worker process) you can change to another valid format, for that same culture in windows and this would now be the outputed format for your web application. 例如,如果您在您的开发机器上运行IIS express(IIS express使用ASP.NET用户进程的Windows用户帐户),您可以更改为另一种有效格式,对于Windows中的相同文化,这将是现在的输出格式您的Web应用程序。

It is hard to give a solution for a non issue. 很难为非问题提供解决方案。 It is still the same date, and actually displays the set culture properly. 它仍然是相同的日期,并且实际上正确地显示了设置文化。

However if you want to force a format, you should be able to do that by setting for example CurrentCulture.DateTimeFormat.ShortDatePattern 但是,如果要强制格式化,则应该可以通过设置例如CurrentCulture.DateTimeFormat.ShortDatePattern

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

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