简体   繁体   English

服务器的全局区域性在不同服务器上不同时的DateTime问题

[英]DateTime issue when global culture of the server is different on different servers

My website is hosted on multiple servers at different locations 我的网站托管在不同位置的多台服务器上

Everywhere the Culture of the data format is different- we use mm/dd/yyyy format every where but incase some server has the culture set to dd/mm/yyyy then our website generates Datetime exception. 到处数据格式的文化都不尽相同-我们在每个地方都使用mm/dd/yyyy格式,但如果某些服务器将文化设置为dd/mm/yyyy则我们的网站会生成Datetime异常。

You should be specifying what culture you want to use whenever you convert a string to a date. 每当将字符串转换为日期时,都应指定要使用的区域性。

The culture you should be using depends on what culture the dates are formatted as. 您应该使用的区域性取决于日期格式的区域性。 For example, if all dates you are parsing are formatted as Slovak : 例如,如果您要解析的所有日期都被格式化为Slovak

String s = "24. 10. 2011";

Then you need to parse the string as though it were in Slovak (Slovakia) ( sk-SK ) culture: 然后,您需要像在斯洛伐克(Slovakia)sk-SK )文化中那样解析字符串:

//Bad:
d = DateTime.Parse(s);

//Good:
d = DateTime.Parse(s, CultureInfo.CreateSpecificCulture("sk-SK")); //Slovak (Slovakia)

If your dates are all in Tajik (Tajikistan Cyrillic) , then you need to parse it as tg-Cryl-Tj : 如果您的日期都在塔吉克斯坦(塔吉克斯坦西里尔文)中 ,则需要将其解析为tg-Cryl-Tj

String s = "24.10.11"

DateTime d = DateTime.Parse(s, CultureInfo.CreateSpecificCulture("tg-Cryl-Tj"));

Which leads to the question: what date format are you using? 这就引出了一个问题:您使用哪种日期格式? You should not be relying on the locale setting of the server, you should be deciding what format you want. 您不应依赖服务器的语言环境设置,而应确定所需的格式。

//Bad
String s = d.ToString();

//Good
String s = d.ToString(CultureInfo.CreateSpecificCulture("si-LK")); //Sinhala (Sri Lanka)

//s = "2011-10-24 12:00:00 පෙ.ව."

i suspect that you prefer to do everything in English. 我怀疑您喜欢用英语做所有事情。 But then you have to decide which variant of English: 但随后您必须确定英语的哪个变体:

  • en-AU (English Austrailia): 24/10/2011 en-AU (英语:澳大利亚): 24/10/2011
  • en-IA (English India): 24-10-2011 en-IA (英语:印度): 24-10-2011
  • en-ZA (English South Africa): 2011/10/24 en-ZA (英语:南非): 2011/10/24
  • en-US (English United States): 10/24/2011 en-USen-US英语): 10/24/2011

i suspect you prefer English (India) ( en-IA ). 我怀疑您更喜欢英语(印度)en-IA )。


But if you really can't decide what culture to use when converting dates to strings and vice-versa, and the dates are never meant to be shown to a user, then you can use the Invariant Culture : 但是,如果您真的无法决定将日期转换为字符串(反之亦然)时要使用哪种文化,并且永远也不想将日期显示给用户,则可以使用Invariant Culture

String s = "10/24/2011" //invariant culture formatted date

d = DateTime.Parse(s, CultureInfo.InvariantCulture); //parse invariant culture date

s = d.ToString(CultureInfo.InvariantCulture); //convert to invariant culture string

Never, ever, store dates internally as strings. 永远不要将日期内部存储为字符串。 Not in the database, not in your app. 不在数据库中,不在您的应用程序中。

If you need to move date values between servers, go binary. 如果需要在服务器之间移动日期值,请使用二进制。 Or if you really really have to use strings, use ToString(CultureInfo.InvariantCulture) - or simply serialize the Ticks property. 或者,如果确实需要使用字符串,请使用ToString(CultureInfo.InvariantCulture) -或简单地序列化Ticks属性。

Also, never pass dates as strings to the database using SQL commands that you build using code. 另外,切勿使用使用代码构建的SQL命令将日期作为字符串传递给数据库。 Use SqlParameter for that, or even better, rely on some O/R Mapper, such as Entity Framework or Linq to SQL. 为此,最好使用SqlParameter ,它依赖于某些O / R映射器,例如Entity Framework或Linq to SQL。

Never rely on the server's default locale. 永远不要依赖服务器的默认语言环境。 For your case, this means: 对于您的情况,这意味着:

  • Use prepared statements where you pass the date as (unformatted) date object and not as (formatted) string object. 使用准备好的语句,将日期作为(未格式化的)日期对象而不是(格式化)字符串对象传递。 You should never use strings to represent dates in your application anyway, as you cannot perform date-specific functions on them (like adding 1 month, getting the last day of the current week, etc.) 无论如何,您永远不应该使用字符串来表示应用程序中的日期,因为您无法在它们上执行特定于日期的功能(例如添加1个月,获取当前星期的最后一天等)。

  • Use SQL functions like to_date and to_char everywhere (exact names depend on your DBMS), if you really need to use string objects in your application 如果您确实需要在应用程序中使用字符串对象,请在各处使用诸如to_dateto_char类的SQL函数(具体名称取决于您的DBMS)

If deployed to a server that's not under your control it's vitally important to make sure your code doesn't have hard-coded dependencies on the culture. 如果将其部署到不受您控制的服务器上,那么确保您的代码不具有对区域性的硬编码依赖性至关重要。

You'll most likely want to search your code for DateTime.Parse or similar. 您很可能希望在代码中搜索DateTime.Parse或类似内容。 We have a set of extension methods on DateTime that we use instead to force the correct culture. 我们在DateTime上有一组扩展方法,可用来强制正确的区域性。

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

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