如何获取本地时间,而不是c#中的服务器时间。 [英] How to get local time, instead of server time in c#.

查看:155
本文介绍了如何获取本地时间,而不是c#中的服务器时间。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我有一个网站,为此,我们的服务器在美国。



当我使用DateTime.Now它给服务器时间,但我想要当地时间,即

我们将仅在印度使用此网站,稍后我们可能会使用它印度。



所以,当我在印度使用这个网站时,我想存储印度时间(IST)。但不是美国服务器时间。



我尝试过:



我的猜测是,使用UTC并在几分钟内获得偏移量。然后将utc与当地时间相匹配。



Dear All,

I have one website, for this, we have server which is in US.

And when i use DateTime.Now it is giving server time, but I want local time, that is,
we will use this site only in india, later we may use it out of india.

So, when i'm using this site in india, then i want to store india time(IST). but not US server time.

What I have tried:

my guessing is, to use UTC and get offset in minutes. And then match the utc with the local time.

What, if i use this code, to get offset in minutes?
//OFFSET IN MINUTES
            TimeZoneInfo AST = TimeZoneInfo.FindSystemTimeZoneById("Arab Standard Time");
            DateTime utc = DateTime.UtcNow;
            DateTime KSA = TimeZoneInfo.ConvertTimeFromUtc(utc, AST);
            double offset = KSA.Subtract(utc).TotalMinutes;





请帮帮我们。





谢谢



please help me guys.


Thanks

推荐答案

始终以UTC格式存储时间。



它使处理时间变得更加容易(相比之下,没有夏令时,通常已知是准确的,因为服务器应该同步他们的时钟,而客户端时钟可能会关闭)。 br />


如果您想稍后显示时间,可以将它们转换为当地时间。如何做到这一点取决于哪个系统生成输出。如果是本地系统(浏览器),您可以使用JavaScript。如果是服务器,则必须知道客户端时区(例如,在用户数据库中存储为字段),并使用服务器上使用的编程语言的转换例程。





对于更新的问题,用一些代码计算偏移量。



如果日期不存储使用JavaScript显示客户端的本地时间。



如果时间存储在服务器上,则使用UTC。然后将其转换为显示,如上所述。但是不要使用计算的偏移量。使用必须为用户存储的时区名称或ID。计算的偏移量可能包括DST(夏令时)偏移量(BTW:可以从TimeZonInfo.BaseUtcOffset属性中检索基本偏移量)。

[/ EDIT]
Always store times in UTC.

It makes handling times much easier (comparable, no daylight saving time, usually known to be exact because servers should sync their clocks while client clocks may be off).

When you want to show times later you might convert them to local time. How to do that depends on which system generates the output. If it is the local system (browser) you can use JavaScript. If it is the server you must know the clients time zone (e.g. stored as field in the user database) and use the conversion routines of the programming languages used on the server.


For the updated question with some code to calculate the offset.

If the date is not going to be stored use JavaScript to show the local time of the client.

If the time is stored on the server use UTC. Then convert it for display as explained above. But don't use a calculated offset. Use the time zone name or ID which must be stored for your users. A calculated offset might include the DST (Daylight Saving Time) offset (BTW: the base offset can be retrieved from the TimeZonInfo.BaseUtcOffset property).
[/EDIT]


您无法直接在C#中获取本地时间,因为它始终在服务器上运行,这是您需要的客户端时间。

您需要做的是使用Javascript访问当地时间: Javascript教程 - 日期 [ ^ ]并将其存储在服务器可以访问的页面上的隐藏字段中。
You can't get the local time directly in C#, as it always runs on the server, and it's the Client time you need.
What you need to do is use Javascript to access the local time: Javascript Tutorial - Date[^] and store it in a hidden field on the page that the server can access.


这篇关于如何获取本地时间,而不是c#中的服务器时间。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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