简体   繁体   English

Web服务器上的DateTime.Now文化/格式

[英]DateTime.Now culture/format on Web Server

For the sake of testing, the Web Server is on a computer with Maori culture where the short date format like this: d/MM/yyyy . 为了进行测试,Web服务器位于Maori文化的计算机上,该计算机的短日期格式为: d/MM/yyyy

The user is on a computer with Hungarian culture and the short date format is yyyy.MM.dd. 用户使用的是Hungarian文化的计算机,短日期格式为yyyy.MM.dd.

I've created a webpage that will display DateTime.Now on a label when the page is loaded. 我创建了一个网页,该网页在加载时将在标签上显示DateTime.Now。 The displayed date is 2/18/2013 2:22:06 PM . 显示的日期是2/18/2013 2:22:06 PM2/18/2013 2:22:06 PM

So now my question is, how come the displayed date is in another format/culture, which I suspect Eng-US? 因此,现在我的问题是,显示日期怎么会以其他格式/文化显示,我怀疑是Eng-US?

Secondly, I will need to compare date from database (assuming the database is on a computer with a different culture) to DateTime.Now. 其次,我需要将数据库中的日期(假设数据库位于具有不同区域性的计算机上)与DateTime.Now进行比较。 To do this, I plan to convert the date from database to match the culture of the Web Server. 为此,我计划转换数据库中的日期以匹配Web服务器的区域性。 But given the situation in my first question, how do I know in which culture/format DateTime.Now will be in? 但是考虑到我的第一个问题,我如何知道DateTime.Now所处的文化/格式?

EDIT: The code used: 编辑:使用的代码:

DateTime nowDateTime = DateTime.Now;
Label7.Text = nowDateTime.ToString().Trim();

For the first part of your question: your code is basically just using the current thread culture. 对于问题的第一部分:您的代码基本上只是使用当前的线程区域性。 You could either make sure that the current thread culture for each request is set to a suitable culture for that request (based on accept headers etc - see HttpRequest.UserLanguages ) or you could pass that explicitly to all calls to ToString etc. 您既可以确保为每个请求当前线程区域性设置为合适的培养请求(基于接受头等等-见HttpRequest.UserLanguages ),或者你可以传递明确所有调用ToString等。

As for why it's coming up in US English format - that may well be the culture that the web-serving service is running as. 至于为什么它以美国英语格式出现-这很可能就是网络服务运行的文化。 That may well not be the same as the culture that the desktop uses. 这很可能与台式机使用的文化不同。 You should take pains to make sure that's not used except possibly as a fallback if the user request doesn't provide any information. 如果用户请求未提供任何信息,您应该尽力确保使用它,除非将其用作备用。 (I'd be tempted to pick an explicit fallback rather than using the default for the service.) (我很想选择一个明确的后备,而不是使用服务的默认值。)

Secondly, I will need to compare date from database (assuming the database is on a computer with a different culture) to DateTime.Now. 其次,我需要将数据库中的日期(假设数据库位于具有不同区域性的计算机上)与DateTime.Now进行比较。

Using DateTime.Now is almost certainly a bad idea. 使用DateTime.Now几乎肯定是一个坏主意。 In most cases, you should use DateTime.UtcNow and store UTC date/times in the database. 在大多数情况下,应使用DateTime.UtcNow并将UTC日期/时间存储在数据库中。 (There are cases where you should instead store a local time and a time zone, but even then you wouldn't use DateTime.Now .) (在某些情况下,您应该存储本地时间和时区,但是即使那样您也不会使用DateTime.Now 。)

To do this, I plan to convert the date from database to match the culture of the Web Server. 为此,我计划转换数据库中的日期以匹配Web服务器的区域性。 But given the situation in my first question, how do I know in which culture/format DateTime.Now will be in? 但是考虑到我的第一个问题,我如何知道DateTime.Now所处的文化/格式?

The culture is primarily relevant when converting to and from string representations. 字符串表示形式进行转换时,文化主要相关。 You should avoid any such conversion when either storing a value in the database or retrieving it. 在数据库中存储值或检索值时,应避免任何此类转换。 Instead, make sure that you're using an appropriate field type in the database ( DATETIME or whatever) and then fetch it as a DateTime in your data access layer. 相反,请确保您在数据库中使用适当的字段类型( DATETIME或其他),然后在数据访问层中将其作为DateTime提取。 If you find yourself trying to use DateTime.Parse or anything similar, that should ring alarm bells. 如果您发现自己尝试使用DateTime.Parse或类似的东西,那应该会发出警报。 You haven't told us what data access technology you're using, but pretty much everything allows you to retrieve a DateTime directly. 您尚未告诉我们您正在使用什么数据访问技术,但是几乎所有内容都允许您直接检索DateTime (eg DbDataReader.GetDateTime .) (例如DbDataReader.GetDateTime 。)

when comparing dates in different time zones it would make sense to store it in the database as UTC. 比较不同时区中的日期时,将其以UTC格式存储在数据库中是有意义的。 Then you could easily compare it with any other date from any time zone (again converted to UTC). 然后,您可以轻松地将其与任何时区中的任何其他日期进行比较(再次转换为UTC)。

Hope this helps. 希望这可以帮助。

Based on your edit, I don't think the culture is really Maori on the relevant thread. 根据您的编辑,我认为相关线程上的文化不是真正的毛利人。 Try this to test: 试试这个测试:

Label7.Text = CultureInfo.CurrentCulture.ToString();

If it is "mi" or "mi-NZ" , it is Maori. 如果是"mi""mi-NZ" ,则为毛利人。 Something with "en" would be English, "hu" Hungarian, and so on. 带有"en"东西将是英语, "hu"匈牙利语,依此类推。

Note that if the current culture is changed programmatically, this affects only the current thread. 请注意,如果以编程方式更改当前区域性,则仅影响当前线程。 Not other threads. 没有其他线程。 But did you try to set the locale in the Control Panel of Windows? 但是您是否尝试在Windows的“控制面板”中设置区域设置?

How come the displayed date is in another format/culture, which I suspect Eng-US? 我怀疑Eng-US的显示日期怎么会以另一种格式/文化显示?

Answer: DateTime.Now which renders 2/18/2013 2:22:06 PM on a label is already in Maori English culture which may be due to current web server configurations or it is picking user's request culture. 答案:在标签上呈现2/18/2013 2:22:06 PM DateTime.Now 已经具有 Maori 英语文化,这可能是由于当前Web服务器配置所致,或者是在选择用户的请求文化。

Possible solution: To render it in client's culture ie Hungarian Culture 可能的解决方案: 将其呈现在客户的文化中,例如匈牙利文化

  1. Easiest and Framework Handles, Modify the configuration to adapt to user's Request Headers 最简单的框架句柄,修改配置以适应用户的请求标头
  2. Manual and you need to code for this, You need to handle Page 's InitializeCulture event and change Culture for the executing HttpRequest based on user's request Header Accept-Language which should be something like Accept-Language: hu-HU,en;q=0.8 手册,你需要为这个代码,你需要处理PageInitializeCulture事件和改变Culture的执行HttpRequest基于用户的请求报头Accept-Language这应该是这样Accept-Language: hu-HU,en;q=0.8

Code Sample for 1 point above 以上1分的代码样本

<system.web>
       <globalization
                  enableclientbasedculture="true"
                  uiculture="auto"
                  culture="auto">
       </globalization>
</system.web>

For this Refer Scott Hanselman's Post on Globalisation of which concept can be applied to Web Forms Applications also. 为此,请参阅Scott Hanselman的“全球化帖子” ,该概念也可以应用于Web窗体应用程序。

For reading database values use code snippet as, 要读取数据库值,请使用以下代码段:

var culture = CultureInfo.CreateSpecificCulture("mi-NZ");
DateTime.Parse(sqlDbReader["MyDateColumnName"], culture)

Refer: Format SqlDataReader as date and MSDN - DateTime.Parse Method 参考:将SqlDataReader格式化为日期MSDN-DateTime.Parse方法

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

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