如果我将 CurrentCultureInfo 设置为 en-us 会发生什么
我正在使用连接到美国远程数据库的客户端应用程序。 结果中出现一些日期时间歧义,例如如果数据库中的日期是 1 月 14 日,则返回为 1 月 13 日。
我想知道的是,如果我将客户端的 CurrentCultureInfo 设置为等于我的服务器,将解决该问题。
如果可以做到这一点..我想要一个相同的小例子。
I am using a client application which connects to a remote Database in US. there are some datetime ambiguities that comes up in the result like if date is 14 Jan in database its returned as 13 Jan.
What I would like to know If I Set the CurrentCultureInfo of my Client equal to my Servers will that solve the issue.
If this can be done..I would like a small example of the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的文化用于格式化。 您遇到的问题似乎是不同时区之一。
我的简单解决方案是始终以 UTC 格式存储日期和时间。 您可以在客户端使用 DateTime.UtcNow,并且可以在必要时稍后将其转换回本地时间。
Your culture is used for formatting. The issue you seem to have is one of different time zones.
My simple solution to this is to always store dates and times in UTC. You can use DateTime.UtcNow for that from the client, and you can convert it back to local time later if necessary.
问题是美国本身有几个不同的时区,所以这不是一个解决方案。 我能想到的一种解决方法是将客户端计算机的时区设置为服务器的时区,但这听起来不太正确。
The problem is the US itself has a few different time zones, so this couldn't be a solution. One workaround I can think of would be to set the time zone of the client computer to that of the server, but something about that just doesn't sound right.
如果您无法将存储的日期更改为 UTC 时间,并在插入和选择数据时来回转换。 然后,除非您的数据被插入到服务器上,否则您将必须记录一些内容来告诉您数据来自哪里(我的意思是插入数据的客户端的本地时区)。
如果您的数据仅插入到服务器中,那么您可以使用 SQL 函数 GetUTCDate 并将其与 GetDate 进行比较,然后从存储在数据库中的日期时间中减去结果。 然后在客户端使用 DateTime.ToLocalTime() 将返回值转换为本地时间。 正如我所说,只有当数据完全插入服务器或至少由同一时区的客户端插入时,这才有效。 不然就忘了吧。
If you can't change the date your storing to a UTC time and convert back and forth when inserting and selecting data. Then unless your data is being inserted at the server you're going to have to record something that tells you where the data came from (i mean what the local time zone is of the client that inserted the data).
If your data is only getting inserted at the server then you can probably use the SQL Functions GetUTCDate and compare it to GetDate, then subtract the result from your datetime stored in the database. Then at the client use DateTime.ToLocalTime() to convert the returned value to local time. As I said this will only work if the data is exclusively inserted at the server or at least by clients in the same time zone. Otherwise forget it.