从存储在SQL数据库上的asp.net应用程序UTC时间显示本地时间 [英] Display local time from utc time stored in sql database on asp.net app

查看:140
本文介绍了从存储在SQL数据库上的asp.net应用程序UTC时间显示本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得保存在UTC格式SQL数据库倍。我在一个gridview显示的时间,但他们仍然是UTC格式。我想将它们转换到客户端的浏览器本地时间。现在的问题是,虽然我可以得到时区偏移,这只是当前的日期/时间。这种偏移可能会改变一些,如果在未来的日期结束了在夏令时存在的。我是比较新的网络编程,但它似乎像什么,我需要做的是运行一些JavaScript作为每个条目结合,不知怎的,取C#DATETIMEOFFSET对象,并将其转换为本地时间GridView的。或者,也许这是不可能的?

I've got times saved in a sql database in utc format. I'm displaying those times on a gridview, however they are still UTC format. I'd like to convert them to the client's browsers local time. The problem is that although I can get the timezone offset that is only for the current date/time. That offset could change if some of those dates in the future end up occuring during daylight savings time. I'm relatively new to web programming but it seems like what I need to do is run some Javascript as each entry binds to the gridview that somehow takes the C# datetimeoffset object and converts it to a local time. Or maybe that's not possible?

推荐答案

这可以在服务器端,如果你有一个的TimeZoneInfo 对象来完成。您可以使用静态 ConvertTimeFromUtc()方法。

This can be done on the server side if you have a TimeZoneInfo object. You can use the static ConvertTimeFromUtc() method.

在C#中:

DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(myDbDateTime, myTimeZoneInfo);

如果你没有服务器端的事情变得棘手,因为JavaScript不提供客户端的时区(除非他们是在美国,即使在当时仅适用于某些浏览器)上的时区。在这种情况下,它可能是最好的以迫使用户选择其当前时区,并将其存储针对他们的帐户。如果这被显示给匿名用户,你应该在UTC默认显示,并提供一个选项,以刷新选定时区。

If you do not have the timezone on the server side things get tricky since javascript does not provide the client's timezone (unless they are in the US, and even then only on some browsers). In this case, it may be best to force the user to select their current timezone and store it against their account. If this is being displayed to anonymous users, you should probably display in UTC by default and provide an option to refresh it for a selected timezone.

更新

有尝试自动确定用户的时区时呈现的几个问题。

There are several issues which appear when trying to automatically determine a user's timezone.


  1. 时区不提供给由用户代理服务器。

  2. JavaScript没有提供对时区(除在一些浏览器,有时)。

JavaScript函数使用getTimezoneOffset()最初可能听起来像一个好主意,但因为有多个时区以相同的偏移,这不是一个独特的价值。许多这些非唯一的区域之间的差别是其实施的夏令时。

The javascript function getTimezoneOffset() may initially sound like a good idea, but since there are multiple timezones with the same offset, this is not a unique value. The difference between many of these non-unique zones is their implementation of daylight saving time.

例如:印第安纳并不认为DST。因此,全年一半的偏移比赛东部时间,而另一半的偏移等于中部时间。

Example: Indiana does not regard DST. Therefore, for half the year their offset matches eastern time, while the other half their offset is equal to central time.

不过,如果你的用户群主要位于美国,并使用IE浏览器,Chrome浏览器,Safari或Firefox的,比你可以使用的toString()方法一个日期对象获取时区。这些浏览器附加时区以不同的方式的日期字符串。在美国之外,在时区不包括在所有的浏览器(虽然有些仍可能显示它)。

If, however, your user base is located primarily in the US and uses IE, Chrome, Safari, or Firefox, than you can use the toString() method on a Date object to obtain the timezone. These browsers append the timezone to the date string in different ways. Outside the US, the timezone is not included in all browsers (though some may still show it).

打开 http://jsbin.com/onulo3 观察:结果
   IE8:太阳2月14日22时12分22秒美国东部时间2010结果
  铬:太阳2010年2月14日22点12分22秒格林尼治标准​​时间0500(东部标准时间)结果
   Safari浏览器:太阳2010年2月14日22点12分22秒格林尼治标准​​时间0500(东部标准时间)结果
  火狐:太阳2010年2月14日22点12分22秒格林尼治标准​​时间0500(东部标准时间)

Open http://jsbin.com/onulo3 to observe:
IE8: Sun Feb 14 22:12:22 EST 2010
Chrome: Sun Feb 14 2010 22:12:22 GMT-0500 (Eastern Standard Time)
Safari: Sun Feb 14 2010 22:12:22 GMT-0500 (Eastern Standard Time)
Firefox: Sun Feb 14 2010 22:12:22 GMT-0500 (Eastern Standard Time)

通过一些分析,现在可以决定你的所有美国用户的时区。对于其他人,您可以显示UTC时间(并通知该效果)。

With some parsing, you can now determine the timezone for all your American users. For everyone else you can display the time in UTC (with a notice to that effect).

这篇关于从存储在SQL数据库上的asp.net应用程序UTC时间显示本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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