如何在不同时区用户的数据库中保留时间 [英] How to keep time in database for different time zone user

查看:68
本文介绍了如何在不同时区用户的数据库中保留时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在server.User中运行的应用程序使用它来自不同的时区。现在我必须节省插入数据的时间。



当用户将数据保存在数据库中我正在做datetime.utcNow()。我认为它将服务器的本地时间(我的应用程序所在的位置)转换为utc时间。我是对的吗?



现在我已经保存了所有用户创建帐户的时区。现在我正在显示他们保存数据的时间(这是在utc的数据库中) )从utc转换到他们当地的时区(从我的帐户中取出他的时区)。



我是否正确?请帮助,实际上如果我是正确的,然后我将这个方法应用于整个项目。

I have an application running in server.User using this are from different time zone.Now I have to save the time of their inserted data.

When user save the data in database I am doing datetime.utcNow().I think it converts the local time of server(in which my application is situated) to utc time. Am I right?

Now I have saved all users' time zone on creating there account.Now I am showing their time of saved data (which is in database in utc) by converting from utc to their local time zone (from his account I'm fetching his time zone).

Am I in right way?please help,actually if I am in right way then I'll apply this approach to the whole project.

推荐答案

如果您使用服务器时区作为转换的基础到UTC,这是一个基于网络的产品,然后不,这是不对的。您需要使用用户时区,并对此进行更正 - 他可能与您的服务器处于不同的时区(我是:我住在英国,我的网站服务器位于美国)。

这应该有所帮助: http://msdn.microsoft.com /en-us/library/bb882561(v=vs.110).aspx [ ^ ] - 它讨论了解决问题的几种方法。
If you are using the server time zone as the "Base" for your conversion to UTC and this is a web-based product, then no, it's not right. You need to use the user time zone, and correct for that - he may well be in a different time zone from your server (I am: I live in the UK, and my website server is in the USA).
This should help: http://msdn.microsoft.com/en-us/library/bb882561(v=vs.110).aspx[^] - it discusses a couple of approaches to the problem.


这篇关于如何在不同时区用户的数据库中保留时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆