如何获取客户端计算机的时区 [英] How to get client machine time zone
本文介绍了如何获取客户端计算机的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好!有谁知道获取客户端计算机时区并将其传递给服务器的方法.我找到了一个脚本,但似乎不起作用.
它既是javascritp还是C#Combo,正是我所寻找的(如果可以的话).附件中可能有脚本可以弄清楚我所缺少的内容.
谢谢
Hello everyone! Does anyone know a way to get the client machine time zone and then pass it to the server. I have found one script but it doesn''t seem to work.
It both a javascritp and C# Combo exactly what I am looking for, If it worked. Attached is the script maybe someone can figure it out what I;m missing.
Thanks
<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>
然后是服务器端
And then Server side
public static int GetTimeZoneOffset(HttpRequest Request)
{
TimeZone tz = TimeZone.CurrentTimeZone;
TimeSpan ts = tz.GetUtcOffset(DateTime.Now);
int result = (int)ts.TotalMinutes;
HttpCookie cookie = Request.Cookies["ClientTimeZone"];
if (cookie != null)
Int32.TryParse(cookie.Value, out result);
return result;
}
推荐答案
也进行检查
根据客户端的时区偏移设置网站DateTime [
Check this too
Setting your website DateTime according to Time Zone Offset of the client[^]
检查该线程
如何获取客户端计算机的时区 [
check this thread
How can i get timezone of the client machine[^]
只需在隐藏的字段中填写客户时区即可.
这是最简单的方法.
像这样的东西:
注意:您可以使用now.toUTCString()或其他任何东西.但老实说,我之前没有使用过"toUTCString"!:confused:
Hi,
just fill the client time zone in a hidden field.
it is the easiest way.
something like this:
Note:you can use now.toUTCString() or any thing else. but to be honest I have not used "toUTCString" before!:confused:
<script>
function GetDate()
{
var now = new Date();
var monthnumber = now.getMonth();
var monthday = now.getDate();
var year = now.getYear();
monthnumber ++;
var sMonth=monthnumber.toString();
var sDay=monthday.toString();
if (monthnumber<10) sMonth ="0" + sMonth;
if (sDay<10) sDay ="0" +sDay;
return year + "/" + sMonth + "/" + sDay;
}
document.getElementById("hdnTime").value=GetDate();
</script>
<input id="hdnTime" type="hidden" value="" />
如果您不想刷新页面,也可以使用xmlhttp将结果发送到服务器.
you can also use xmlhttp to send the result to the server if you want not to refresh your page.
这篇关于如何获取客户端计算机的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文