我需要知道客户的时间。我的做法是将偏移量保留在cookie中,然后计算。我的问题是我还需要插入 cookie,即使在加载第一页时也是如此。我知道有很多方法,但没有人回答需要。我需要在加载第一页之前使用本地时间。所以我不能使用JavaScript。
我试图在帖子中将用户发送回客户端,放入cookie然后将其返回给服务器,但这对谷歌来说是有问题的,因为他们没有cookie。
这是功能:
public static DateTime? GetClientTime()
{
HttpRequest Request = HttpContext.Current.Request;
if (Request.Cookies["DynoOffset"] != null)
{
string strOffset = Request.Cookies["DynoOffset"].Value;
int offset = int.Parse(strOffset);
TimeZone localZone = TimeZone.CurrentTimeZone;
DateTime currentDate = DateTime.Now;
DateTime CreationDate = localZone.ToUniversalTime(currentDate).AddMinutes(-offset);
return CreationDate;
}
else
{
StoreClientTime();
return null;
}
}
public static DateTime? StoreClientTime()
{
var Context = HttpContext.Current;
var Session = Context.Session;
var Response = Context.Response;
var Request = Context.Request;
// if the local time is not saved yet in Session and the request has not posted the localTime
if (Request.Cookies["DynoOffset"] == null && String.IsNullOrEmpty(Request.Params["localTime"]))
{
// then clear the content and write some html a javascript code which submit the local time
Response.ClearContent();
Response.Write("<form id='local' method='post' name='local'>" +
"<script src=\"/Js/jquery-1.7.1.min.js\" type=\"text/javascript\"></script>" +
"<script src=\"/Js/JqueryUI/jquery.cookie.js\" type=\"text/javascript\"></script>" +
"<script type=\"text/javascript\">" +
"$.cookie(\"DynoOffset\", new Date().getTimezoneOffset(), { expires: 150 });" +
"$(\"#local\").submit()" +
"</script>" +
"</form>");
//
Response.Flush();
// end the response so PageLoad, PagePreRender etc won't be executed
Response.End();
return null;
}
else
{
return GetClientTime().Value;
}
}
我想根据calture找到偏移量,但我不知道该怎么做。