日期时间问题,当服务器的全球文化是不同服务器上的不同 [英] DateTime issue when global culture of the server is different on different servers
问题描述
我的网站是在不同地点的多台服务器托管
My website is hosted on multiple servers at different locations
无处不在的数据格式的文化是different-我们使用 MM / DD / YYYY
格式,每一个地方,但柜面一些服务器具有文化设置为 DD / MM / YYYY
那么,我们的网站会生成日期时间例外。
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.
推荐答案
您应该指定你想,只要你将字符串转换为日期使用什么样的文化。
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:
String s = "24. 10. 2011";
然后,你需要分析字符串,就好像是在斯洛伐克(斯洛伐克)( SK-SK
)的文化:
//Bad:
d = DateTime.Parse(s);
//Good:
d = DateTime.Parse(s, CultureInfo.CreateSpecificCulture("sk-SK")); //Slovak (Slovakia)
如果您的日期都在塔吉克斯坦(塔吉克斯坦西里尔文),然后你需要分析它作为 TG-Cryl-TJ
:
If your dates are all in Tajik (Tajikistan Cyrillic), then you need to parse it as 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 පෙ.ව."
我怀疑你preFER用英语做的一切。但你必须决定哪些英语变种:
i suspect that you prefer to do everything in English. But then you have to decide which variant of English:
-
EN-AU
(英文Austrailia):24/10/2011
-
EN-IA
(英文印度):2011-10-24
-
EN-ZA
(英文南非):2011年10月24日
-
的en-US
(美国英语):10/24/2011
en-AU
(English Austrailia):24/10/2011
en-IA
(English India):24-10-2011
en-ZA
(English South Africa):2011/10/24
en-US
(English United States):10/24/2011
我怀疑你preFER 英语(印度)( EN-IA
)。
i suspect you prefer English (India) (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:
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
这篇关于日期时间问题,当服务器的全球文化是不同服务器上的不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!