如何创建不属于当前区域性的 DateTime 对象

发布于 2024-09-11 20:44:13 字数 1259 浏览 2 评论 0原文

我在这方面真的是一片空白。我一直致力于全球化,但 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 dt3 = Convert.ToDateTime(dt2, us_culture); 
        DateTime dt3 = DateTime.Parse(dt2, us_culture);

但是两者都会产生 de-DE DateTimes。我问这个问题的动机是其余的业务逻辑将调用 dt2.toString() 并会导致不正确的日期时间字符串。我意识到我可以将 toString() 更改为 toString(us_culture) 但我不想更改所有其余的业务逻辑来适应此更改。

有没有办法在 CurrentThread 的文化之外的文化中获取 DateTime ?

谢谢你的宝贵时间,我已经为此烦恼太久了。

I'm really drawing a blank on this one. I've been working on globalization but the DateTime seems to always revert back to the CurrentThread's culture. I broke this down into smaller steps hoping to find my issue, but it's starting to drive me crazy.

I've got a textbox with the date expressed as a string:

        // 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);

At this point I want a DateTime that's in en-US
I've tried both:

        DateTime dt3 = Convert.ToDateTime(dt2, us_culture); 
        DateTime dt3 = DateTime.Parse(dt2, us_culture);

But both produce de-DE DateTimes. My motivation in asking this question is the rest of the business logic is going to be calling dt2.toString() and will result in an incorrect date time string. I realize I could change toString() to be toString(us_culture) but I'd rather not change all of the rest of the business logic to accomodate this change.

Is there a way to get a DateTime in a culture other than the CurrentThread's culture?

Thank you for your time, I've been banging my head against this for too long.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

燃情 2024-09-18 20:44:13

DateTime 对象就是一个日期/时间,它与文化无关。如果您想要的话,您需要使用特定的区域性来格式化日期。

A DateTime object is just that - a date/time, it's culture agnostic. You'll need to format the dates using a specific culture if that's what you're after.

_失温 2024-09-18 20:44:13

ToString() 的无参数重载将始终根据 CurrentCulture 设置输出日期。这是您想要的 100 的 99 倍。

如果您不想使用重载,您可以手动将 CurrentThread.CurrentCulture 设置为您想要的。

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");

但请理解,这会改变您应用程序中的所有内容。

The parameter less overload of ToString() will always output the date according to the CurrentCulture setting. This is what you want 99 times of 100.

If you don't want to use the overload you could manually set the CurrentThread.CurrentCulture to what you want.

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");

But understand that this will change for everything in your application.

迷途知返 2024-09-18 20:44:13

尝试使用自定义数据格式:

string dt2 = dt.ToString("m/d/yyyy");

try using custom data format:

string dt2 = dt.ToString("m/d/yyyy");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文