Date.ToShortDateFormat() 有时使用错误的区域设置?
我将 VS 2008 项目转换为 VS 2010,但保留在 .NET 3.5 框架上。我没有在应用程序中的任何位置设置我的区域设置。
我有几台运行 Windows 7 和 XP 的计算机,并且都将区域设置为 EN-AU。
有时我的应用程序会返回短日期格式,例如 MM/dd/YY(EN-US)。一旦退出并重新启动,它就会恢复为 AU(dd/mm/yy) 的正确格式。同样,我没有在项目中的任何地方设置我的区域设置。 (我有一些以 EN-US 作为语言的 RDLC 报告)
为什么会发生这种情况?我也尝试显式设置线程区域设置。但同样的行为。
I converted my VS 2008 project into vs 2010 but kept it on .NET 3.5 framework. I don't set my locale anywhere within the app.
I've got a couple on computers running windows 7 and XP and both have the region set to EN-AU.
Sometime my app returns the short date format like MM/dd/YY (EN-US). As soon as you quit it and start again it reverts to the proper format for AU(dd/mm/yy). Again I'm not setting my locale anywhere in the project. (I have some RDLC reports which have EN-US as the language though)
Any reason why this would be happening? I tried setting the Thread locale explicitly as well. But same behaviour.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Datetime.UTC 基于本地系统时间以及本地系统是否遵守夏令时。
无论您是否设置位置。日期时间选择器应根据 UTC 和您的系统自动设置。
查看此链接,它提供了一些有关日期时间的有用提示
http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/6b3b1e95-e044-46db-94ba-0e75fcf9d2b2/
Datetime.UTC is based on local system time and whether the local system is observing daylight savings time.
Regardless whether you set your location. The datetime picker should automatically set itself based on UTC and your system.
Check out this link which gives some helpful hints to datetime
http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/6b3b1e95-e044-46db-94ba-0e75fcf9d2b2/
这很奇怪。
我建议您不要使用 ToShortDate() 因为我已经看到了其中的几个问题。相反,我建议使用以下代码:
string formattedDate = someDateTime.ToString("d", CultureInfo.CurrentCulture);
这基本上是一样的。您可以引入常量而不是“d”以提高可读性(它是短日期格式化字符串)。
传递 IFormatProvider 始终是一个好习惯,因为它作为注释清楚地记录了您的假设(在上面的示例中我说:这是我想要呈现给用户的日期字符串;如果我想使用这个日期进行进一步处理,通过网络等发送。我会使用 CultureInfo.InvariantCulture)。
This is strange.
I would advice you not to use ToShortDate() as I have seen couple of issues from it. Instead, I recommend using following code:
string formattedDate = someDateTime.ToString("d", CultureInfo.CurrentCulture);
This basically does the same. And you can introduce constant instead of "d" for greater readability (it is short date formatting string).
It is always good practice to pass IFormatProvider, as it works as a comment clearly documenting your assumptions (in the example above I said: this is the date string I want to present to user; if I wanted to use this date for further processing, send it via network, etc. I would use CultureInfo.InvariantCulture).