亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在ASP.NET中獲取客戶端日期和時間?

如何在ASP.NET中獲取客戶端日期和時間?

www說 2019-12-02 13:15:09
我使用DateTime.Now服務器時會得到日期和時間。有什么方法可以在ASP.NET中獲取客戶端日期和時間?
查看完整描述

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

請記住要使用分鐘,因為并非所有時區都是整個小時。


查看完整回答
反對 回復 2019-12-02
  • 3 回答
  • 0 關注
  • 914 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號