在WCF服务中捕获客户端Datetimeoffset
我有一个网络服务,可以保存世界各地用户的一些事件。用户稍后可以登录报告系统并查看他们的事件。我需要以当地时间显示事件时间戳。
我可以在 WCF 服务
后面的 sql server 2008
中使用 DateTimeOffset
数据库字段。但我对如何在网络服务上转换它感到困惑。当客户端调用我的 Web 服务事件方法时,他们需要发送日期时间参数,因为实际事件的时间可能与服务接收它的时间不同。
如果我尝试在服务器上将其转换为 DateTimeOffset dtOffset = eventTime;
,它将根据服务器的时间进行转换。
我怎样才能以正确的方式做到这一点?
i have a web service that saves some events from users all over the world. The users can later log in to a report system and view their events. I would need to display the event timestamps in their local time.
I can use the DateTimeOffset
db field in sql server 2008
behind the WCF service
. But I'm confused about how to convert this on the web service. When the client calls my web service event method, they need to send the datetime parameter because the time of the actual event may be different from the time service receives it.
if I try to convert it on the server as DateTimeOffset dtOffset = eventTime;
, it is going to convert it based on the server's time.
How can I do this the right way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该以 UTC 格式将时间存储在数据库中,并且无论您想要在哪里显示时间,只需使用 DateTime 对象的 ToLocalTime() 将 UTC 时间转换为客户端计算机上的时区即可。
You should store time in UTC format in the database and where ever you want to display the time, just use ToLocalTime() of DateTime object to convert the UTC time to the time zone on the client machine.