在客户端-服务器应用程序中处理时间
对于我正在开发的 iPhone 聊天应用程序,我想知道如何处理所有时区问题。 我的意思是如何使当 client1 向 client2 发送消息时,他们都可以看到消息旁边的正确时间(日期)。
到目前为止,这就是我认为我需要做的:
- 客户端1向服务器发送消息,客户端1使用它的本地时间来显示消息旁边的日期。
- 服务器(c#)获取消息并将其保存到数据库中,同时使用
DateTime.UtcNow
将当前时间保存在消息旁边。 - 当client2登录时,他从服务器获取带有UTC时间的消息,并将其转换为本地时间。
这是这样做的方法吗? 将日期从服务器发送到客户端的最佳方式是什么?在服务器中,日期作为 DateTime
对象保存到数据库中,我可以将其转换为什么格式以将其发送到客户端?
For an iPhone chat app I'm working on, I want to know how to handle all the timezone issue.
I mean how to make that when client1 sends a message to client2, they both see the correct time (date) next to the message.
So far this is what i think i need to do:
- Client1 send message to server, client1 use it's local time to present the date next to the message.
- The server(c#) gets the message and save it to the database, also he save the current time next to the message with
DateTime.UtcNow
. - When client2 login, he get the message from the server with the UTC time, and convert it to its local time.
Is this the way to do that?
What is the best way to send the date from server to client? In the server the date is save to the database as DateTime
object, to what format can I convert it to send it to the client?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有几个选项,具体取决于报告时间需要的准确程度:
关于来回发送日期,请查看 NSDateFormatter。您可以使用可以轻松使用的格式,或者在服务器上执行任何最简单的操作,并使用格式化程序上的自定义
dateFormat
属性来根据需要进行解析。You have a couple options here depending on how accurate the reported time needs to be:
In terms of sending dates back and forth, check out the documentation on NSDateFormatter. You can use a format that it can consume easily or do whatever's easiest on the server and use a custom
dateFormat
property on the formatter to parse as you please.