3 回答

TA貢獻(xiàn)1776條經(jīng)驗(yàn) 獲得超12個(gè)贊
我喜歡使用瀏覽器/系統(tǒng)時(shí)間和時(shí)區(qū)或讓他們選擇其時(shí)區(qū)的想法。在過(guò)去的項(xiàng)目中,我使用了以下內(nèi)容:
<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>
然后在服務(wù)器上:
/// <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參數(shù)傳遞并在Page_Load中進(jìn)行處理:
http://host/page.aspx?tz=-360
請(qǐng)記住要使用分鐘,因?yàn)椴⒎撬袝r(shí)區(qū)都是整個(gè)小時(shí)。
- 3 回答
- 0 關(guān)注
- 912 瀏覽
添加回答
舉報(bào)