【发布时间】:2010-07-29 18:57:27
【问题描述】:
我真的在这个上画了一个空白。我一直致力于全球化,但 DateTime 似乎总是回归到 CurrentThread 的文化。我把它分解成更小的步骤,希望能找到我的问题,但它开始让我发疯。
我有一个以字符串形式表示日期的文本框:
// the CurrentThread's culture is de-DE
// My test browser is also set to de-DE
IFormatProvider culture = new System.Globalization.CultureInfo("de-DE",
true);
// en-US culture, what I'd ultimately like to see the DateTime in
IFormatProvider us_culture = new System.Globalization.CultureInfo("en-US",
true);
// correctly reads the textbox value (22.7.2010 into a datetime)
DateTime dt= DateTime.Parse(txtStartDate.Text, culture,
System.Globalization.DateTimeStyles.NoCurrentDateDefault);
// correctly produces a string 7/22/2010
string dt2 = dt.ToString(us_culture);
此时我想要一个位于 en-US 的 DateTime 我都试过了:
DateTime dt3 = Convert.ToDateTime(dt2, us_culture);
DateTime dt3 = DateTime.Parse(dt2, us_culture);
但两者都产生 de-DE DateTimes。我提出这个问题的动机是其余的业务逻辑将调用 dt2.toString() 并将导致不正确的日期时间字符串。我意识到我可以将 toString() 更改为 toString(us_culture) 但我宁愿不更改所有其余的业务逻辑来适应这种变化。
有没有办法在 CurrentThread 的文化之外的文化中获取 DateTime?
感谢您的宝贵时间,我已经为此苦苦挣扎了太久了。
【问题讨论】:
-
我不完全理解。你想要一个不同的 DateTime 值,还是字符串返回不正确(如果我只是很密集,请道歉)。无论文化如何,DateTime 都是 DateTime。唯一会改变的是字符串表示。
标签: c# asp.net datetime globalization culture