Display local time from utc time stored in sql database on asp.net app

…衆ロ難τιáo~ 提交于 2019-12-04 08:15:37

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

In C#:

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

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.

Update

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

  1. Timezone is not provided to the server by the user agent.
  2. Javascript does not provide access to the timezone (except in some browsers, sometimes).

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.

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.

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).

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)

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).

I found the following on "Indiana Daylight Savings Time": http://www.timetemperature.com/tzus/indiana_time_zone.shtml

As of now, 1/18/2010, a Microsoft system library call TimeZoneInfo.ConvertTimeFromUtc seems to reflect this behavior, am checking ..

Robert L

Here are two ways to convert.

Way #1: If you have the datetime in seconds-since-epoch format, just use something like:

 var d=new Date(seconds_since_epoch);
 document.write(d.toString());

Way #2: If you have only the year, month, day, hour, minute, and second in UTC time, use:

 var d=new Date(yyyy,mo-1,dd,hh,mi,ss); // in JavaScript, January is month 0
 document.write(d.toString());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!