3 回答

TA貢獻1776條經驗 獲得超12個贊
我喜歡使用瀏覽器/系統時間和時區或讓他們選擇其時區的想法。在過去的項目中,我使用了以下內容:
<script language="javascript">
function checkClientTimeZone()
{
// Set the client time zone
var dt = new Date();
SetCookieCrumb("ClientDateTime", dt.toString());
var tz = -dt.getTimezoneOffset();
SetCookieCrumb("ClientTimeZone", tz.toString());
// Expire in one year
dt.setYear(dt.getYear() + 1);
SetCookieCrumb("expires", dt.toUTCString());
}
// Attach to the document onload event
checkClientTimeZone();
</script>
然后在服務器上:
/// <summary>
/// Returns the client (if available in cookie) or server timezone.
/// </summary>
public static int GetTimeZoneOffset(HttpRequest Request)
{
// Default to the server time zone
TimeZone tz = TimeZone.CurrentTimeZone;
TimeSpan ts = tz.GetUtcOffset(DateTime.Now);
int result = (int) ts.TotalMinutes;
// Then check for client time zone (minutes) in a cookie
HttpCookie cookie = Request.Cookies["ClientTimeZone"];
if (cookie != null)
{
int clientTimeZone;
if (Int32.TryParse(cookie.Value, out clientTimeZone))
result = clientTimeZone;
}
return result;
}
或者,您可以將其作為URL參數傳遞并在Page_Load中進行處理:
http://host/page.aspx?tz=-360
請記住要使用分鐘,因為并非所有時區都是整個小時。
- 3 回答
- 0 關注
- 914 瀏覽
添加回答
舉報